I created a helper function get_node()
for a common pattern, but for some reason it leads to borrow errors, but when copy pasting its contents it does not. I've marked the problematic lines with comments:
type NodeMap = HashMap<i32, Node>;
pub trait IdGenerator {
fn gen(&mut self) -> i32;
}
pub struct Tree<'a> {
active: i32,
nodes: NodeMap,
generator: &'a mut dyn IdGenerator,
}
impl<'a> Tree<'a> {
pub fn create_sibling(&mut self) {
let active = self.nodes.get(&self.active).unwrap(); // <-- THIS WORKS
// let active = self.get_node(self.active); // <-- THIS FAILS
let mut node = Node::new(self.generator.gen(), active.parent);
// [......]
}
fn get_node(&self, id: i32) -> &Node {
self.nodes.get(&id).unwrap()
}
}
Uncommenting and commenting the two let active...
lines leads to the following error:
error[E0502]: cannot borrow `*self.generator` as mutable because it is also borrowed as immutable
--> src/tree_new.rs:35:34
|
34 | let active = self.get_node(self.active);
| ---- immutable borrow occurs here
35 | let mut node = Node::new(self.generator.gen(), active.parent);
| ^^^^^^^^^^^^^^^^^^^^ ------------- immutable borrow later used here
| |
| mutable borrow occurs here
The error messages makes sense, but what I'm confused on is why the same borrowing logic doesn't apply to the original code without the helper function.