error[E0500]: closure requires unique access to `*self` but it is already borrowed
19 | self.actions.iter_mut().for_each(|a| a.act(self));
| ------------ -------- ^^^ ---- second borrow occurs due to use of `*self` in closure
| | | |
| | | closure construction occurs here
| | first borrow later used by call
| borrow occurs here
I understand the borrowing problem, but I don't know what's the best way to fix it. For now, I'm doing:
fn act(&mut self) {
let actions = std::mem::replace(&mut self.actions, Vec::new());
for mut action in actions {
action.act(self);
self.actions.push(action);
}
}
@2e71828's solution is right if you don't need the Actions to have access to an Actor with actions, but your approach above can work if they do (maybe an action can add further actions). In that case you can neaten it a little using std::mem::take and it can be written in one line like the original version if you want: