Skip to content

Instantly share code, notes, and snippets.

@wrightmikea
Created February 18, 2020 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrightmikea/0b5766c89b8b3194040d8d9deda3ea44 to your computer and use it in GitHub Desktop.
Save wrightmikea/0b5766c89b8b3194040d8d9deda3ea44 to your computer and use it in GitHub Desktop.
mutating a hashmap in a struct
use std::collections::HashMap;
#[derive(Debug)]
pub struct Element {
pub a: u8,
}
#[derive(Debug)]
pub struct HSS {
pub store: HashMap<u32, Vec<Element>>,
}
fn main() {
let ve = vec![Element{a:1}, Element{a:2}];
let mut hm = HashMap::new();
hm.insert(42, ve);
let mut hss = HSS { store: hm };
hss.store.insert(19, vec![Element{a:3}]);
println!("{:?}", hss);
}
// > ./main
// HSS { store: {42: [Element { a: 1 }, Element { a: 2}], 19: [Element { a: 3 }]} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment