This question already has an answer here:
- Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time 4 answers
I want to find some node in the tree and I need a pointer to the container of nodes: &mut Vec<Node>
struct Node {
c: Vec<Node>,
v: i32,
}
impl Node {
pub fn new(u: i32, n: Node) -> Node {
let mut no = Node {
c: Vec::new(),
v: u,
};
no.c.push(n);
no
}
}
fn main() {
let mut a = Node::new(1,
Node::new(2,
Node::new(3,
Node::new(4,
Node::new(5,
Node {
c: Vec::new(),
v: 6,
})))));
let mut p: &mut Vec<Node> = &mut a.c;
while p.len() > 0 {
p = &mut p[0].c;
}
p.push(Node {
c: Vec::new(),
v: 7,
});
}