Skip to content

Instantly share code, notes, and snippets.

@nlinker
Created July 3, 2018 20:41
Show Gist options
  • Save nlinker/2879f2ec6aede83318bf70a3d3eed867 to your computer and use it in GitHub Desktop.
Save nlinker/2879f2ec6aede83318bf70a3d3eed867 to your computer and use it in GitHub Desktop.
move values from map to vector
// https://users.rust-lang.org/t/predictable-random-numbers/17593
extern crate rand;
use std::collections::HashMap;
use rand::Rng;
use rand::IsaacRng;
#[derive(Debug, Clone)]
struct Value(Vec<u8>);
fn main() {
let mut rng = IsaacRng::new_from_u64(123);
let np = 4;
let mut the_map: HashMap<u8, Vec<u8>> = HashMap::new();
for k in 0..np {
the_map.entry(k).or_insert_with(|| vec![]);
}
for (_, mut body) in &mut the_map {
let num = rng.gen_range(0, 6);
for _ in 0..num {
let x = rng.gen_range(0, 10);
body.push(x);
}
}
fn convert(m: &mut HashMap<u8, Vec<u8>>) -> Vec<Value> {
let mut vs = Vec::<Value>::new();
for k in 0..m.len() {
// vs.push(Value(m[&(k as u8)].clone()));
vs.push(Value(m.remove(&(k as u8)).unwrap()));
}
vs
}
eprintln!("{:?}", the_map);
let values = convert(&mut the_map);
eprintln!("{:?}", values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment