Why does calling boxed closures work just like calling closures? Is it because of Deref implementation of Box ? Would it work on all smart pointers? As I tried some examples to better understand it, I get the following error.
fn main() {
let mut x = String::new();
let y = || {
let t = &mut x;
};
let mut ww = Box::new(y);
(&mut ww)();
}
This throws an error
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
--> src/main.rs:22:13
|
22 | let y = || {
| ^^ this closure implements `FnMut`, not `Fn`
23 | let t = &mut x;
| - closure is `FnMut` because it mutates the variable `x` here
...
26 | (&mut ww)();
| ----------- the requirement to implement `Fn` derives from here
Any documentation on how boxed closures work would be helpful as well.
from Recent Questions - Stack Overflow https://ift.tt/3IfvkoQ
https://ift.tt/eA8V8J
No comments:
Post a Comment