Hi,
I have been having a hard time trying to get a HashMap which includes a HashMap to work as expected (assuming it is at all possible). It could very well be a gap in my knowledge regarding ownership which I'm still very much getting my head around.
I am trying to create a structure that looks something like...
{
"Thing": { 123: [4,5,6] },
"Other": { 789: [10,11,12] }
}
I have written an example of my current attempt which has errors.
use std::collections::HashMap;
struct Test {
nested_hashmap: HashMap<String, HashMap<i32, Vec<(i32)>>>,
}
fn main() {
let mut test = Test { nested_hashmap: HashMap::new() };
test.nested_hashmap.entry("Foo".to_string()).or_insert(HashMap::new());
for (id, &thing) in test.nested_hashmap.iter() {
thing.entry(1).or_insert(Vec::new());
}
}
which produces the following error
test.rs:20:9: 20:14 error: cannot borrow immutable local variable `thing` as mutable
test.rs:20 thing.entry(1).or_insert(Vec::new());
^~~~~
note: in expansion of for loop expansion
test.rs:19:5: 21:6 note: expansion site
test.rs:19:14: 19:20 error: cannot move out of borrowed content
test.rs:19 for (id, &thing) in test.nested_hashmap.iter() {
^~~~~~
note: in expansion of for loop expansion
test.rs:19:5: 21:6 note: expansion site
test.rs:19:15: 19:20 note: attempting to move value to here
test.rs:19 for (id, &thing) in test.nested_hashmap.iter() {
^~~~~
note: in expansion of for loop expansion
test.rs:19:5: 21:6 note: expansion site
test.rs:19:15: 19:20 help: to prevent the move, use `ref thing` or `ref mut thing` to capture value by reference
Thanks in advance any help is greatly appreciated as well as any recommendations for things I should be reading in order to close some gaps in my knowledge (especially if you see any big incorrect assumptions above).