UPDATE: Mentoring instructions found in this comment below.
The following code fails because the closure moves out of vec, and anything implementing Fn only gets a reference to its environment (i.e can't move self)
fn get_closure() -> Box<Fn() -> Vec<u8>> {
let vec = vec![1u8, 2u8];
let closure = move || {
vec
};
Box::new(closure)
}
A simple fix that would allow the closure to properly implement Fn is to replace vec with vec.clone() inside the closure. The error message, however, is of no help:
error: the trait `core::ops::Fn<()>` is not implemented for the type `[closure <anon>:6:24: 8:6]` [E0277]
<line #> Box::new(closure)
^~~~~~~~~~~~~~~~~
In cases where the closure is more complex than this trivial example, the error message is no more descriptive than above, making it much more difficult to debug.
I would like to see something more along the lines of:
error: the trait `core::ops::Fn<()>` is not implemented for the type `[closure <anon>:6:24: 8:6]`
<line #> note: closure moves out of environment: vec
^~~
UPDATE: Mentoring instructions found in this comment below.
The following code fails because the closure moves out of
vec, and anything implementingFnonly gets a reference to its environment (i.e can't moveself)A simple fix that would allow the closure to properly implement
Fnis to replacevecwithvec.clone()inside the closure. The error message, however, is of no help:In cases where the closure is more complex than this trivial example, the error message is no more descriptive than above, making it much more difficult to debug.
I would like to see something more along the lines of: