Skip to content

Commit

Permalink
Generate README with cargo-rdme
Browse files Browse the repository at this point in the history
  • Loading branch information
pczarn committed Feb 13, 2023
1 parent 352e063 commit da57c28
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,59 @@ If you want to use bit-vec in a program that has `#![no_std]`, just drop default
bit-vec = { version = "0.6", default-features = false }
```

<!-- cargo-rdme -->
<!-- cargo-rdme start -->

### Examples

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

[sieve]: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

```rust
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);
```

<!-- cargo-rdme end -->
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2020 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2023 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -25,8 +25,6 @@
// (4) `BitSet` is tightly coupled with `BitVec`, so any changes you make in
// `BitVec` will need to be reflected in `BitSet`.

//! Collections implemented with bit vectors.
//!
//! # Examples
//!
//! This is a simple example of the [Sieve of Eratosthenes][sieve]
Expand Down

0 comments on commit da57c28

Please sign in to comment.