Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use std::collections::BitSet in stable Rust?

Tags:

rust

I am trying to use BitSet data structure, but it gives me a compile error saying it was not able to find the BitSet. Has std::collections::BitSet been released in the stable version?

use std::collections::BitSet;

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

Produces the error:

error[E0432]: unresolved import `std::collections::BitSet`
 --> src/main.rs:1:5
  |
1 | use std::collections::BitSet;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ no `BitSet` in `collections`
like image 557
kumarmo2 Avatar asked Dec 17 '17 08:12

kumarmo2


1 Answers

It seems that BitSet existed in Rust 1.3.0, which is very old, but was already deprecated at that time and finally removed by this commit.

Instead, you can use bit-set crate, as suggested by the deprecation message above. There's also 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);
}

You'll have to add a dependency to the bit-set crate in some way. It's easy if you're using Cargo:

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

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

If you're using the official Rust Playground, you can automatically use bit-set, because it is one of the top 100 downloaded crates or a dependency of one of them.

like image 75
Masaki Hara Avatar answered Oct 21 '22 11:10

Masaki Hara