如何在稳定的Rust中使用std :: collections :: BitSet?

问题描述 投票:5回答:1

我正在尝试使用BitSet数据结构,但它给了我一个编译错误,说它无法找到BitSet.已经在稳定版本中发布了std::collections::BitSet

use std::collections::BitSet;

fn main() {
    println!("Hello, world!");
}

产生错误:

error[E0432]: unresolved import `std::collections::BitSet`
 --> src/main.rs:1:5
  |
1 | use std::collections::BitSet;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ no `BitSet` in `collections`
rust
1个回答
5
投票

似乎BitSet existed in Rust 1.3.0, which is very old,但当时已经弃用,最后removed by this commit

相反,您可以使用bit-set crate,如上面的弃用消息所示。还有documentation

extern crate bit_set;

use bit_set::BitSet;

fn main() {
    let mut s = BitSet::new();
    s.insert(32);
    s.insert(37);
    s.insert(3);
    println!("s = {:?}", s);
}

您必须以某种方式向bit-set箱添加依赖项。如果你使用货物很容易:

[package]
name = "foo"
version = "0.1.0"
authors = ["Foo Bar <[email protected]>"]

[dependencies]
bit-set = "0.4.0" # Add this line

如果你正在使用the official Rust Playground,你可以自动使用bit-set,因为它是the top 100 downloaded crates之一或其中一个的依赖。

© www.soinside.com 2019 - 2024. All rights reserved.