I'm still a little confused on how the Fn-FnMut-FnOnce thing works with other traits. I want to eliminate the Copy constraint from the following function for traversing a tree structure.
pub fn for_each<F>(&mut self, mut f: F)
where
F: FnMut(&mut Tree<T>) + Copy,
{
self.children.iter_mut().for_each(|c| c.for_each(f));
f(self);
}
The reason for this is that I am trying to pass a closure that modifies an outside variable to for_each and the Copy prevents that (E0277). However, when I get rid of the Copy, I get the following error message:
error[E0507]: cannot move out of `f`, a captured variable in an `FnMut` closure
--> src/tree.rs:34:58
|
30 | pub fn for_each<F>(&mut self, mut f: F)
| ----- captured outer variable
...
34 | self.children.iter_mut().for_each(|c| c.for_each(f));
| ^ move occurs because `f` has type `F`, which does not implement the `Copy` trait
error[E0382]: borrow of moved value: `f`
--> src/tree.rs:35:9
|
30 | pub fn for_each<F>(&mut self, mut f: F)
| ----- move occurs because `f` has type `F`, which does not implement the `Copy` trait
...
34 | self.children.iter_mut().for_each(|c| c.for_each(f));
| --- - variable moved due to use in closure
| |
| value moved into closure here
35 | f(self);
| ^ value borrowed here after move
|
help: consider further restricting this bound
|
32 | F: FnMut(&mut Tree<T>) + Copy,
| ^^^^^^
error: aborting due to 2 previous errors
How can I solve this issue? I'm also open to turning this into an iterator if that makes it easier.