Difficulty with cyclic structs


I don't quite get why rust compiler can't compile this code:

code linkhttps://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=90141e10693b7a815f7e1036dbd92dda

Try changing the trait you're using for the callback to FnOnce. The Fn trait implies the closure can be called multiple times without mutating state. If proxy is moved into the closure and then dropped, it could not be called multiple times (you can think of it "spending" proxy, and thus it can only be called once). If you try be more explicit about your intention to move proxy you get a more helpful error message:

                let proxy = proxy;
error[E0507]: cannot move out of `proxy`, a captured variable in an `Fn` closure
  --> src/main.rs:13:29
10 |         let proxy = self.proxy;
   |             ----- captured outer variable
13 |                 let proxy = proxy;
   |                             ^^^^^
   |                             |
   |                             move occurs because `proxy` has type `std::boxed::Box<dyn std::ops::Fn(std::boxed::Box<dyn std::ops::Fn()>)>`, which does not implement the `Copy` trait
   |                             help: consider borrowing here: `&proxy`

Changing the trait bound to FnOnce will allow the move of proxy the way you seem to intend.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.