error[E0507]: cannot move out of `clone`, a captured variable in an `Fn` closure
--> src/lib.rs:7:35
|
5 | let clone = paths_from.clone();
| ----- captured outer variable
6 | move |s| {
7 | get_cpy_dialog_content(s, clone)
| ^^^^^ move occurs because `clone` has type `Vec<(String, String, usize)>`, which does not implement the `Copy` trait
E0507’s explanationdoes mention closure bodies, but there’s definitely room for improvement. The error message itself is pretty fine IMO. I don’t think that it’s necessarily the job of the error message to explain why one “cannot move out of a captured variable in an Fn closure”. That’s just a fact, the error code explanation should handle the rest. A separate error code for moving out of captured variables in non-FnOnce closures could help.
Well, actually ... the error message itself could be rephrased to be more clear about that it’s a fact that “it’s impossible to move out of a captured variable in an Fn closure”. I don’t know, maybe: cannot move out of `clone` because it’s a captured variable in an `Fn` closure.
Next to the options listed in the explanation
Try to avoid moving the variable. not possible when calling get_cpy_dialog_content
Somehow reclaim the ownership. e.g. with the extra .clone() call
Implement the Copy trait on the type. impossible for Vec
there’s also the option of downgrading from Fn to FnOnce, in case you don’t actually need an Fn when calling get_content_clone.
maybe that’s not an option for you though; depends on how (and in particular how often) you want to call the function returned from a get_content_clone call.
I actually still don't fully understand why it works.
The move keyword
specifies that we move from outside of the closure to the closure, so the closure becomes the owner of those things that were moved into.
If that's correct, the move happens during first call... So the next call don't have anything to move.
Why this works?