17 releases

Uses old Rust 2015

0.6.2 May 6, 2020
0.6.1 Jun 1, 2019
0.6.0 May 16, 2019
0.5.1 Mar 21, 2019
0.4.0 Jul 10, 2015

#7 in Data structures

Download history 49743/week @ 2020-06-28 53712/week @ 2020-07-05 54339/week @ 2020-07-12 56742/week @ 2020-07-19 59607/week @ 2020-07-26 60818/week @ 2020-08-02 55513/week @ 2020-08-09 53954/week @ 2020-08-16 56902/week @ 2020-08-23 55166/week @ 2020-08-30 55310/week @ 2020-09-06 59477/week @ 2020-09-13 61851/week @ 2020-09-20 62963/week @ 2020-09-27 61346/week @ 2020-10-04 57174/week @ 2020-10-11

236,699 downloads per month
Used in 1,085 crates (102 directly)

MIT/Apache

79KB
1.5K SLoC

A Vec of bits.

Documentation is available at https://contain-rs.github.io/bit-vec/bit_vec.

Build Status crates.io

Usage

Add this to your Cargo.toml:

[dependencies]
bit-vec = "0.6"

and this to your crate root:

extern crate bit_vec;

If you want serde support, include the feature like this:

[dependencies]
bit-vec = { version = "0.6", features = ["serde"] }

If you want to use bit-vec in a program that has #![no_std], just drop default features:

[dependencies]
bit-vec = { version = "0.6", default-features = false }

lib.rs:

Collections implemented with bit vectors.

Examples

This is a simple example of the Sieve of Eratosthenes which calculates prime numbers up to a given limit.

use bit_vec::BitVec;

let max_prime = 10000;

// Store the primes as a BitVec
let primes = {
    // Assume all numbers are prime to begin, and then we
    // cross off non-primes progressively
    let mut bv = BitVec::from_elem(max_prime, true);

    // Neither 0 nor 1 are prime
    bv.set(0, false);
    bv.set(1, false);

    for i in 2.. 1 + (max_prime as f64).sqrt() as usize {
        // if i is a prime
        if bv[i] {
            // Mark all multiples of i as non-prime (any multiples below i * i
            // will have been marked as non-prime previously)
            for j in i.. {
                if i * j >= max_prime {
                    break;
                }
                bv.set(i * j, false)
            }
        }
    }
    bv
};

// Simple primality tests below our max bound
let print_primes = 20;
print!("The primes below {} are: ", print_primes);
for x in 0..print_primes {
    if primes.get(x).unwrap_or(false) {
        print!("{} ", x);
    }
}
println!();

let num_primes = primes.iter().filter(|x| *x).count();
println!("There are {} primes below {}", num_primes, max_prime);
assert_eq!(num_primes, 1_229);

Dependencies

~195KB