Created
February 18, 2020 08:10
-
-
Save wrightmikea/0b5766c89b8b3194040d8d9deda3ea44 to your computer and use it in GitHub Desktop.
mutating a hashmap in a struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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