In my program I have a system where there is a list of objects, each of which stores a list of objects. (Kind of like a tree). Whenever these objects need to update a function is run that recursively goes through all the lists. It looks a bit like this:
struct Object {
objects: Vec<Object>
}
impl Object {
fn update(&mut self, parent: &mut Object) {
for object in self.objects.iter_mut() {
object.update(self);
}
}
}
However, the rust borrow checker prevents this:
35 | for object in self.objects.iter_mut() { // Loop through each sub object
| ---------------------------
| |
| first mutable borrow occurs here
| first borrow later used here
36 | object.update(self); // Update it
|
What would be the preferred way to do this. If you think this is an XY problem please tell me.
EDIT: The objects need their parents for accessing some of the other parameters. (Not shown here)