I need a class with functionality equal to vector<bool>
in C++. The Rust documentation tells about BitVec
, but use std::collections::BitVec
causes Unresolved import error during compiling. According to a pull request, BitVec
has been removed. Is there any adequate replacement for it?
Asked
Active
Viewed 1.8k times
17

Shepmaster
- 388,571
- 95
- 1,107
- 1,366

Vercetti
- 437
- 1
- 6
- 17
-
https://crates.io/search?q=BitVec ? – ildjarn May 01 '17 at 10:00
-
"*Rust documentation tells about BitVec*" -> could you link to where the documentation says that? Maybe it's indeed a documentation bug. – Lukas Kalbertodt May 01 '17 at 10:06
-
https://doc.rust-lang.org/1.2.0/std/collections/struct.BitVec.html – Vercetti May 01 '17 at 10:08
-
11.2.0... _Years_ old. ;-] – ildjarn May 01 '17 at 10:09
-
@ildjarn, so the onliest way is to use it via Cargo.toml? – Vercetti May 01 '17 at 10:11
-
@Vercetti You can use crates without Cargo, FWIW. You're just advised not to. – Veedrac May 01 '17 at 13:14
1 Answers
30
There does not exist a dedicated bit-vector in the standard library and Vec<bool>
is not specialized like C++'s vector<bool>
. Rust advocates the use of external crates instead of building a huge standard library. The de-facto crate for this use case is bit-vec
.
You appear to have found a link to an old standard library documentation: https://doc.rust-lang.org/1.2.0/std/collections/struct.BitVec.html
. Note the 1.2.0
in the url! The current version of Rust is 1.25 (as of April 2018), which means that 1.2
is already more than two years old. Apart from that, BitVec
is marked as unstable in the 1.2 docs; it was removed later.

Lukas Kalbertodt
- 79,749
- 26
- 255
- 305
-
7*`Vec
` is not specialized like C++'s `vector – Shepmaster May 01 '17 at 13:10`* — thankfully. That was not a great decision: let's have a `vector` that *behaves differently*. -
2Just a heads up: this crate "is in maintenance mode, due to insufficient maintainer resources" – Alex Oct 02 '17 at 03:15
-
6There's also the confusingly named `bitvec` crate which I've found to be somewhat more featureful. https://docs.rs/bitvec – Aleksandar Dimitrov Jul 27 '20 at 19:25
-
1IMHO, to prefer random crates instead of a comprehensive (and curated) standard library is wasting anyones time. Moving targets. Crates falling in and out of favor... In Common Lisp I have bit-vector right out of the box and I do not need to go on a scavenger hunt... – BitTickler May 03 '22 at 18:59
-
@AleksandarDimitrov I tried to use that bitvec crate and it has horrible usability. I ended up rolling my own for my specific use case. (`arr.get(i).as_deref() == Some(&true)`... really=?! lol). – BitTickler May 04 '22 at 12:11