Closed
Description
This code manipulates a HashMap using the new Entry API, checking for consistency after each step.
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Vacant, Occupied};
use std::rand::Rng;
fn check(m: &HashMap<int, ()>) {
for k in m.keys() {
assert!(m.contains_key(k),
"{} is in keys() but not in the map?", k);
}
}
fn main() {
let mut m = HashMap::new();
let mut rng = std::rand::weak_rng();
// Populate the map with some items.
for _ in range(0u, 50) {
let x = rng.gen_range(-10, 10);
m.insert(x, ());
}
for i in range(0u, 1000) {
let x = rng.gen_range(-10, 10);
match m.entry(x) {
Vacant(_) => {},
Occupied(e) => {
println!("{}: remove {}", i, x);
e.take();
},
}
check(&m);
}
}
The assertion in check
should never fail, but in my tests it does so reliably, almost always within the first 10 iterations of the second loop. When it does fail, the value of k
is not one of the values previously removed during the loop (that is, keys
is right to return this key, and contains_key
is wrong to report it as absent).
I tested this with rustc 0.13.0-dev (377d7524a 2014-11-24 13:56:36 +0000)
on 64-bit Linux.
Activity
sfackler commentedon Nov 25, 2014
cc @gankro
Gankra commentedon Nov 25, 2014
Fffffff--- looking into it now.
cc @pczarn
Gankra commentedon Nov 25, 2014
Oh thank god it was a simple stupid mistake, and not something complicated.
Make HashMap::take not corrupt the map. Fixes rust-lang#19292
Make HashMap::take not corrupt the map. Fixes rust-lang#19292
Merge pull request rust-lang#19292 from Veykril/push-zonnrrlosqmv