Que vidas eu uso para criar estruturas Rust que se referenciam umas às outras ciclicamente?
Eu gostaria de ter membros struct que conheçam seus pais. Isso é aproximadamente o que estou tentando fazer:
struct Parent<'me> {
children: Vec<Child<'me>>,
}
struct Child<'me> {
parent: &'me Parent<'me>,
i: i32,
}
fn main() {
let mut p = Parent { children: vec![] };
let c1 = Child { parent: &p, i: 1 };
p.children.push(c1);
}
Eu tentei apaziguar o compilador com vidas sem entender completamente o que estava fazendo.
Aqui está a mensagem de erro em que estou preso:
error[E0502]: cannot borrow `p.children` as mutable because `p` is also borrowed as immutable
--> src/main.rs:13:5
|
12 | let c1 = Child { parent: &p, i: 1 };
| - immutable borrow occurs here
13 | p.children.push(c1);
| ^^^^^^^^^^ mutable borrow occurs here
14 | }
| - immutable borrow ends here
Isso faz algum sentido, mas não tenho certeza de onde ir a partir daqui.