I am aware what this error means, I just don't have any idea how to solve this puzzle. As a short background - I need a tree structure in my app, I've discovered Rust doesn't play well with circular references, hence I am trying to implement a tree-like data structure myself (but the borrow checker doesn't let me do that either):
struct Node {
children: Vec<i32>,
}
let mut hm = HashMap::new();
hm.insert(1, Node { children: vec![] });
hm.insert(2, Node { children: vec![1] });
// Let's say I want to remove node 2...
let node = match hm.get(&2) {
Some(node) => node,
None => panic!(""),
};
// ...but first I want to remove its children:
for removable in &node.children {
match hm.get(&removable) {
Some(_) => hm.remove(&removable), // cannot borrow `hm` as mutable because it is also borrowed as immutable
None => panic!("Oops, trying to remove data that's not there"),
};
}
How to solve that?