Hi,
I want to go through a tree like structure which consists of nodes which can have multiple other nodes. I would like also modify these nodes. Here is my minimal example:
pub struct Node {
id: i32,
node_vec: Vec<Node>,
}
impl Node {
fn new(id: i32) -> Node {
Node {
id: id,
node_vec: Vec::new(),
}
}
fn get_vec(&mut self) -> &mut Vec<Node> {
&mut self.node_vec
}
}
fn main() {
let mut node1 = Node::new(1);
node1.get_vec().push(Node::new(2));
let mut vec_ref = node1.get_vec();
let depth = 2;
let mut current_depth = 1;
while current_depth < depth {
vec_ref = vec_ref.iter_mut().last().unwrap().get_vec();
current_depth += 1;
}
}
When I try to go deep in structure and get mut ref as shown above I get error:
error[E0506]: cannot assign to `vec_ref` because it is borrowed
--> src/main.rs:27:9
|
27 | vec_ref = vec_ref.iter_mut().last().unwrap().get_vec();
| ^^^^^^^^^^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | borrow of `vec_ref` occurs here
| assignment to borrowed `vec_ref` occurs here
error[E0499]: cannot borrow `*vec_ref` as mutable more than once at a time
--> src/main.rs:27:19
|
27 | vec_ref = vec_ref.iter_mut().last().unwrap().get_vec();
| ^^^^^^^ mutable borrow starts here in previous iteration of loop
...
30 | }
| - mutable borrow ends here
I am new in rust programming so can anyone help me correct my code or explain how such problem should be resolved properly in rust?