I wanted to use some random numbers for a benchmark:
#![feature(test)]
/// benchmarks for different map implementation
extern crate test;
extern crate rand;
use test::Bencher;
use rand::Rng;
#[bench]
fn setup_random_hashmap(b: &mut Bencher) {
let mut val : u32 = 0;
let mut rng = rand::thread_rng();
let mut map = std::collections::HashMap::new();
b.iter(|| { map.insert(rng.gen::<u32>(), val); val += 1; })
}
However, rustc comes back with:
bench.rs:14:16: 14:32 error: unresolved name `rand::thread_rng`
bench.rs:14 let mut rng = rand::thread_rng();
thread_rng is defined in rand
as a pub fn
. What declaration am I missing? I am using 1.2.0-nightly (8f9f2fe97 2015-06-07)
.
You can reproduce this in Rust 1.0 (and at least up through 1.13):
With the same error:
To fix this, you need to add the
rand
crate to your Cargo.toml:This is due to the fact that Rust has an internal, hidden crate also called
rand
. When the 1.0 stability guarantees were looming, many of these mostly-internal crates were spun out to their own repos and hosted on crates.io. However, the compiler still needed parts of these crates, and so stubs of them were left in the compiler, but marked private.A clever idea that appears to work! For future people, this only applies to nightly builds, and that means you accept whatever breaking code might happen!
The biggest downside is that every run of the program will have the same sequence of numbers, because it will always have the same seed. This may be a benefit for benchmarking though!