Description
This library exposes a vector called BoolVec which allows you to store 8 booleans in a single byte. Basically a boolean only occupies a single bit.
How to use
Please check out the documentation on docs.rs and the examples below
Examples
Initializing an empty BoolVec
To create a new BoolVec you can use either BoolVec::new() or BoolVec::default():
use BoolVec;
let bv1 = new;
let bv2 = default;
assert_eq!;
Or, if you already know your desired capacity you can use BoolVec::with_capacity(cap):
use BoolVec;
let bv = with_capacity;
assert!;
Initializing BoolVec from a Vec or slice
You can initialize a BoolVec from anything that implements AsRef<[bool]> with BoolVec::from(S).
This includes vectors and slices:
use BoolVec;
let bv1 = from;
let bv2 = from;
assert_eq!;
Initializing using boolvec![] macro
Just like Vec with the vec![] macro, you can initialize a BoolVec with the boolvec![] macro:
use ;
let bv1 = new;
let bv2 = boolvec!;
assert_eq!;
let bv3 = boolvec!;
let bv4 = boolvec!;
assert_eq!;
Pushing values into the BoolVec
You can push booleans to the back of the BoolVec just like you would with a normal Vec:
use boolvec;
let mut bv = boolvec!;
bv.push;
assert_eq!;
Popping values off the BoolVec
Again, just like with a normal Vec, you can remove items at the end of a BoolVec with BoolVec.pop().
Do note that just like with Vec, removed values will be returned. If no value is found, None is returned instead:
use boolvec;
let mut bv1 = boolvec!;
let mut bv2 = boolvec!;
assert_eq!;
assert_eq!;
assert_eq!;
Getting values from a BoolVec
You can get a value from a BoolVec with the BoolVec.get(index) method.
None will be returned if index is invalid:
use boolvec;
let bv = boolvec!;
assert_eq!;
assert_eq!;
Changing values in a BoolVec
You can change the value of any bool inside a BoolVec with the BoolVec.set(index, value) method.
Just like with BoolVec.get(index), None will be returned if index is invalid.
use boolvec;
let mut bv = boolvec!;
assert_eq!;
assert_eq!;
assert_eq!;
Negating values in a BoolVec
Negating a value is simple with the BoolVec.negate(index) method.
This will update your value in the BoolVec,
changing it either from true to false, or from false to true, and then return the negated value.
Again, None will be returned if index is invalid.
use boolvec;
let mut bv = boolvec!;
assert_eq!;
assert_eq!;
assert_eq!;
Getting a Vec from a BoolVec
You can get a Vec<bool> from a BoolVec with the BoolVec.into_vec() method:
use boolvec;
let bv = boolvec!;
let vector = vec!;
assert_eq!;
WARNING: It's recommended to try and work with BoolVec when possible. Converting to Vec<bool> might drastically increase your memory usage
Iteration
You can iterate using a for loop or convert your BoolVec into a BoolVecIter directly using BoolVec.into_iter():
use boolvec;
let bv = boolvec!;
for boolean in &bv
let mut bv_iter = bv.into_iter;
while let Some = bv_iter.next
Printing
You can either debug print and pretty print your BoolVec:
use boolvec;
let bv = boolvec!;
println!;
println!; // This will print up to 8 booleans in a single line
Or print the underlying bytes of your BoolVec:
use boolvec;
let mut bv = boolvec!;
bv.set.unwrap;
assert_eq!
It's ok if you don't understand this le latter, it's mostly for debug purposes and you don't need to concern with it.
Other
Other methods you might already know from Vec are implemented, such as:
BoolVec.len()to get the current length of theBoolVec;BoolVec.capacity()to get the capacity;BoolVec.is_empty()to check whether theBoolVecis empty or not;