Why can't get moving closure's reference

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`
1 Like

The first two don't capture anything, and can be const promoted to statics.

The last one captures a value dynamically and can't be promoted. Pushing a reference to it into the Vec is similarto trying to return a reference to a local variable,