i can't understand why line 4 and line 5 is ok, but line 6 reported error.
from my understanding, moving closure is independent from context.
1. fn boxed_closure(closures: &mut Vec<&dyn Fn()>) {
2. let v = "abc".to_string();
3. let v = 1;
4. closures.push(&||println!("first"));
5. closures.push(&|| println!("second"));
6. closures.push(&move || println!("v's value:{}",v));
7. }
8.
9. fn main() {
10. let mut closures:Vec<&dyn Fn()> = vec![];
11.
12. boxed_closure(&mut closures);
13. }
the error is:
1 | fn boxed_closure(closures: &mut Vec<&dyn Fn()>) {
| - let's call the lifetime of this reference `'1`
...
6 | closures.push(&move || println!("v's value:{}",v));
| ---------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'1`