bit-vec 0.1.0 failed to build bit-vec-0.1.0
Please check the build logs and, if you believe this is' fault, open an issue.
Visit the last successful build: bit-vec-0.6.2

Collections implemented with bit vectors.


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

# #![feature(collections, core, step_by)]
use std::collections::{BitSet, BitVec};
use std::iter;

let max_prime = 10000;

// Store the primes as a BitSet
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 iter::range_inclusive(2, (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 * i..max_prime).step_by(i) { bv.set(j, false) }

// 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.contains(&x) {
        print!("{} ", x);

// We can manipulate the internal BitVec
let num_primes = primes.get_ref().iter().filter(|x| *x).count();
println!("There are {} primes below {}", num_primes, max_prime);