I have a tree-walking code:
fn append_point(&mut self, coords: Point, data: T) -> Result<(), &str>{
match &mut self.node{
QuadTreeNode::None => {
self.node = QuadTreeNode::Leaf(coords, data);
Ok(())
}
QuadTreeNode::Node(quadrants) => {
for quadrant in quadrants.iter_mut(){
if quadrant.is_inside(coords){
return quadrant.append_point(coords, data);
}
}
Err("Point is not in any of quardrants")
}
It does not compile, complaining
error[E0506]: cannot assign to `self.node` because it is borrowed
--> src/quadtree.rs:95:17
|
89 | fn append_point(&mut self, coords: Point, data: T) -> Result<(), &str>{
| - let's call the lifetime of this reference `'1`
...
93 | match &mut self.node{
| -------------- borrow of `self.node` occurs here
94 | QuadTreeNode::None => {
95 | self.node = QuadTreeNode::Leaf(coords, data);
| ^^^^^^^^^ assignment to borrowed `self.node` occurs here
...
101 | return quadrant.append_point(coords, data);
| ----------------------------------- returning this value requires that `self.node` is borrowed for `'1
If I comment out any actions for Leaf part (for
loop), it compiles with no issues. The same is for self.node
, if I comment it out, code compiles.
I don't understand why borrow checker is not happy here. It's a match statement, it's either one branch or other. They are never executed simultaneously, so they should not influence each other.
Is it borrowck imperfection or I miss something important here? And how to deal with those types of things?
Full code is here: equart/quadtree.rs at 111ea3a9c06ef2669993a7cb4e6816364097a94a · amarao/equart · GitHub