Struct BitVec
struct BitVec<B = u32> { ... }
The bitvector type.
Examples
use BitVec;
let mut bv = from_elem;
// insert all primes less than 10
bv.set;
bv.set;
bv.set;
bv.set;
println!;
println!;
// flip all values in bitvector, producing non-primes less than 10
bv.negate;
println!;
println!;
// reset bitvector to empty
bv.clear;
println!;
println!;
Implementations
impl BitVec<u32>
fn new() -> SelfCreates an empty
BitVec.Examples
use BitVec; let mut bv = new;fn from_elem(nbits: usize, bit: bool) -> SelfCreates a
BitVecthat holdsnbitselements, setting each element tobit.Examples
use BitVec; let mut bv = from_elem; assert_eq!; for x in bv.iterfn with_capacity(nbits: usize) -> SelfConstructs a new, empty
BitVecwith the specified capacity.The bitvector will be able to hold at least
capacitybits without reallocating. Ifcapacityis 0, it will not allocate.It is important to note that this function does not specify the length of the returned bitvector, but only the capacity.
fn from_bytes(bytes: &[u8]) -> SelfTransforms a byte-vector into a
BitVec. Each byte becomes eight bits, with the most significant bits of each byte coming first. Each bit becomestrueif equal to 1 orfalseif equal to 0.Examples
use BitVec; let bv = from_bytes; assert!;fn from_fn<F>(len: usize, f: F) -> Self where F: FnMut(usize) -> boolCreates a
BitVecof the specified length where the value at each index isf(index).Examples
use BitVec; let bv = from_fn; assert!;
impl<B: BitBlock> BitVec<B>
fn blocks(self: &Self) -> Blocks<'_, B>Iterator over the underlying blocks of data
fn storage(self: &Self) -> &[B]Exposes the raw block storage of this
BitVec.Only really intended for
BitSet.unsafe fn storage_mut(self: &mut Self) -> &mut Vec<B>Exposes the raw block storage of this
BitVec.Safety
Can probably cause unsafety. Only really intended for
BitSet.fn get(self: &Self, i: usize) -> Option<bool>Retrieves the value at index
i, orNoneif the index is out of bounds.Examples
use BitVec; let bv = from_bytes; assert_eq!; assert_eq!; assert_eq!; // Can also use array indexing assert_eq!;unsafe fn get_unchecked(self: &Self, i: usize) -> boolRetrieves the value at index
i, without doing bounds checking.For a safe alternative, see
get.Safety
Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
Examples
use BitVec; let bv = from_bytes; unsafefn get_mut(self: &mut Self, index: usize) -> Option<MutBorrowedBit<'_, B>>Retrieves a smart pointer to the value at index
i, orNoneif the index is out of bounds.Examples
use BitVec; let mut bv = from_bytes; *bv.get_mut.unwrap = true; *bv.get_mut.unwrap = false; assert!; assert_eq!;unsafe fn get_unchecked_mut(self: &mut Self, index: usize) -> MutBorrowedBit<'_, B>Retrieves a smart pointer to the value at index
i, without doing bounds checking.Safety
Calling this method with out-of-bounds
indexmay cause undefined behavior even when the result is not used.Examples
use BitVec; let mut bv = from_bytes; unsafe assert_eq!;fn set(self: &mut Self, i: usize, x: bool)Sets the value of a bit at an index
i.Panics
Panics if
iis out of bounds.Examples
use BitVec; let mut bv = from_elem; bv.set; assert_eq!;fn set_all(self: &mut Self)Sets all bits to 1.
Examples
use BitVec; let before = 0b01100000; let after = 0b11111111; let mut bv = from_bytes; bv.set_all; assert_eq!;fn negate(self: &mut Self)Flips all bits.
Examples
use BitVec; let before = 0b01100000; let after = 0b10011111; let mut bv = from_bytes; bv.negate; assert_eq!;fn union(self: &mut Self, other: &Self) -> boolCalculates the union of two bitvectors. This acts like the bitwise
orfunction.Sets
selfto the union ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different lengths.
Examples
use BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01111110; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn intersect(self: &mut Self, other: &Self) -> boolCalculates the intersection of two bitvectors. This acts like the bitwise
andfunction.Sets
selfto the intersection ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different lengths.
Examples
use BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01000000; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn or(self: &mut Self, other: &Self) -> boolCalculates the bitwise
orof two bitvectors.Sets
selfto the union ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different lengths.
Examples
use BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01111110; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn and(self: &mut Self, other: &Self) -> boolCalculates the bitwise
andof two bitvectors.Sets
selfto the intersection ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different lengths.
Examples
use BitVec; let a = 0b01100100; let b = 0b01011010; let res = 0b01000000; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn difference(self: &mut Self, other: &Self) -> boolCalculates the difference between two bitvectors.
Sets each element of
selfto the value of that element minus the element ofotherat the same index. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different length.
Examples
use BitVec; let a = 0b01100100; let b = 0b01011010; let a_b = 0b00100100; // a - b let b_a = 0b00011010; // b - a let mut bva = from_bytes; let bvb = from_bytes; assert!; assert_eq!; let bva = from_bytes; let mut bvb = from_bytes; assert!; assert_eq!;fn xor(self: &mut Self, other: &Self) -> boolCalculates the xor of two bitvectors.
Sets
selfto the xor ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different length.
Examples
use BitVec; let a = 0b01100110; let b = 0b01010100; let res = 0b00110010; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn nand(self: &mut Self, other: &Self) -> boolCalculates the nand of two bitvectors.
Sets
selfto the nand ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different length.
Examples
use BitVec; let a = 0b01100110; let b = 0b01010100; let res = 0b10111011; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn nor(self: &mut Self, other: &Self) -> boolCalculates the nor of two bitvectors.
Sets
selfto the nor ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different length.
Examples
use BitVec; let a = 0b01100110; let b = 0b01010100; let res = 0b10001001; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn xnor(self: &mut Self, other: &Self) -> boolCalculates the xnor of two bitvectors.
Sets
selfto the xnor ofselfandother. Both bitvectors must be the same length. Returnstrueifselfchanged.Panics
Panics if the bitvectors are of different length.
Examples
use BitVec; let a = 0b01100110; let b = 0b01010100; let res = 0b11001101; let mut a = from_bytes; let b = from_bytes; assert!; assert_eq!;fn all(self: &Self) -> boolReturns
trueif all bits are 1.Examples
use BitVec; let mut bv = from_elem; assert_eq!; bv.set; assert_eq!;fn count_ones(self: &Self) -> u64Returns the number of ones in the binary representation.
Also known as the Hamming weight.
Examples
use BitVec; let mut bv = from_elem; assert_eq!; bv.set; assert_eq!;fn count_zeros(self: &Self) -> u64Returns the number of zeros in the binary representation.
Also known as the opposite of Hamming weight.
Examples
use BitVec; let mut bv = from_elem; assert_eq!; bv.set; assert_eq!;fn iter(self: &Self) -> Iter<'_, B>Returns an iterator over the elements of the vector in order.
Examples
use BitVec; let bv = from_bytes; assert_eq!;fn iter_mut(self: &mut Self) -> IterMut<'_, B>Returns an iterator over mutable smart pointers to the elements of the vector in order.
Examples
use BitVec; let mut a = from_elem; a.iter_mut.enumerate.for_each; assert!;fn append(self: &mut Self, other: &mut Self)Moves all bits from
otherintoSelf, leavingotherempty.Examples
use BitVec; let mut a = from_bytes; let mut b = from_bytes; a.append; assert_eq!; assert_eq!; assert!;fn split_off(self: &mut Self, at: usize) -> SelfSplits the
BitVecinto two at the given bit, retaining the first half in-place and returning the second one.Panics
Panics if
atis out of bounds.Examples
use BitVec; let mut a = new; a.push; a.push; a.push; a.push; let b = a.split_off; assert_eq!; assert_eq!; assert!; assert!;fn none(self: &Self) -> boolReturns
trueif all bits are 0.Examples
use BitVec; let mut bv = from_elem; assert_eq!; bv.set; assert_eq!;fn any(self: &Self) -> boolReturns
trueif any bit is 1.Examples
use BitVec; let mut bv = from_elem; assert_eq!; bv.set; assert_eq!;fn to_bytes(self: &Self) -> Vec<u8>Organises the bits into bytes, such that the first bit in the
BitVecbecomes the high-order bit of the first byte. If the size of theBitVecis not a multiple of eight then trailing bits will be filled-in withfalse.Examples
use BitVec; let mut bv = from_elem; bv.set; assert_eq!; let mut bv = from_elem; bv.set; bv.set; assert_eq!;fn eq_vec(self: &Self, v: &[bool]) -> boolCompares a
BitVecto a slice ofbools. Both theBitVecand slice must have the same length.Panics
Panics if the
BitVecand slice are of different length.Examples
use BitVec; let bv = from_bytes; assert!;fn truncate(self: &mut Self, len: usize)Shortens a
BitVec, dropping excess elements.If
lenis greater than the vector's current length, this has no effect.Examples
use BitVec; let mut bv = from_bytes; bv.truncate; assert!;fn reserve(self: &mut Self, additional: usize)Reserves capacity for at least
additionalmore bits to be inserted in the givenBitVec. The collection may reserve more space to avoid frequent reallocations.Panics
Panics if the new capacity overflows
usize.Examples
use BitVec; let mut bv = from_elem; bv.reserve; assert_eq!; assert!;fn reserve_exact(self: &mut Self, additional: usize)Reserves the minimum capacity for exactly
additionalmore bits to be inserted in the givenBitVec. Does nothing if the capacity is already sufficient.Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer
reserveif future insertions are expected.Panics
Panics if the new capacity overflows
usize.Examples
use BitVec; let mut bv = from_elem; bv.reserve; assert_eq!; assert!;fn capacity(self: &Self) -> usizeReturns the capacity in bits for this bit vector. Inserting any element less than this amount will not trigger a resizing.
Examples
use BitVec; let mut bv = new; bv.reserve; assert!;fn grow(self: &mut Self, n: usize, value: bool)Grows the
BitVecin-place, addingncopies ofvalueto theBitVec.Panics
Panics if the new len overflows a
usize.Examples
use BitVec; let mut bv = from_bytes; bv.grow; assert_eq!; assert_eq!;fn pop(self: &mut Self) -> Option<bool>Removes the last bit from the
BitVec, and returns it. ReturnsNoneif theBitVecis empty.Examples
use BitVec; let mut bv = from_bytes; assert_eq!; assert_eq!; assert_eq!;fn push(self: &mut Self, elem: bool)Pushes a
boolonto the end.Examples
use BitVec; let mut bv = new; bv.push; bv.push; assert!;fn len(self: &Self) -> usizeReturns the total number of bits in this vector
unsafe fn set_len(self: &mut Self, len: usize)Sets the number of bits that this
BitVecconsiders initialized.Safety
Almost certainly can cause bad stuff. Only really intended for
BitSet.fn is_empty(self: &Self) -> boolReturns true if there are no bits in this vector
fn clear(self: &mut Self)Clears all bits in this vector.
fn shrink_to_fit(self: &mut Self)Shrinks the capacity of the underlying storage as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the underlying storage that there is space for a few more elements/bits.
fn insert(self: &mut Self, at: usize, bit: bool)Inserts a given bit at index
at, shifting all bits after by onePanics
Panics if
atis out of bounds forBitVec's length (that is, ifat > BitVec::len())Examples
use BitVec; let mut b = new; b.push; b.push; b.insert; assert!;Time complexity
Takes O(
len) time. All items after the insertion index must be shifted to the right. In the worst case, all elements are shifted when the insertion index is 0.
impl<B> Freeze for BitVec<B>
impl<B> RefUnwindSafe for BitVec<B>
impl<B> Send for BitVec<B>
impl<B> Sync for BitVec<B>
impl<B> Unpin for BitVec<B>
impl<B> UnsafeUnpin for BitVec<B>
impl<B> UnwindSafe for BitVec<B>
impl<B: BitBlock> Clone for BitVec<B>
fn clone(self: &Self) -> Selffn clone_from(self: &mut Self, source: &Self)
impl<B: BitBlock> Debug for BitVec<B>
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl<B: BitBlock> Default for BitVec<B>
fn default() -> Self
impl<B: BitBlock> Display for BitVec<B>
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl<B: BitBlock> Eq for BitVec<B>
impl<B: BitBlock> Extend for BitVec<B>
fn extend<I: IntoIterator<Item = bool>>(self: &mut Self, iterable: I)
impl<B: BitBlock> FromIterator for BitVec<B>
fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self
impl<B: BitBlock> Hash for BitVec<B>
fn hash<H: hash::Hasher>(self: &Self, state: &mut H)
impl<B: BitBlock> Index for BitVec<B>
fn index(self: &Self, i: usize) -> &bool
impl<B: BitBlock> IntoIterator for BitVec<B>
fn into_iter(self: Self) -> IntoIter<B>
impl<B: BitBlock> Ord for BitVec<B>
fn cmp(self: &Self, other: &Self) -> Ordering
impl<B: BitBlock> PartialEq for BitVec<B>
fn eq(self: &Self, other: &Self) -> bool
impl<B: BitBlock> PartialOrd for BitVec<B>
fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>
impl<T> Any for BitVec<B>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for BitVec<B>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for BitVec<B>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for BitVec<B>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for BitVec<B>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for BitVec<B>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for BitVec<B>
fn to_string(self: &Self) -> String
impl<T, U> Into for BitVec<B>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for BitVec<B>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for BitVec<B>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>