Why a Fn is required to be 'static when it is captured by another closure?


Considering following code

fn foo<F>(f:Box<F>)->Box<FnMut()->i32>
where F:Fn()->i32+'static   ///why F is required to be 'static? remove it and the compile will complain
    let result=Box::new(move||{   //f is captured by move here

fn bar()->Box<FnMut()->i32>{

fn main() {
    let mut ff=bar();

The generic parameter F is required to be 'static.

My questions are:

  1. The input parameter f of function foo is boxed and captured by the inner closure result with the move keyword, why its type F (actually Box<F>) is required to be 'static? It is passed by value and it is moved into the closure result, i.e., its ownership is transferred into the result, why its lifetime has to be 'static?

  2. In above code, the function foo is called in the function bar, then the input parameter of foo is apparently stack allocated, the lifetime of which is obviously not 'static, but above program can be compiled and run, why? What does the 'static mean in above program?


The Box<...> result has an implicit bound of 'static. You can add an arbitrary lifetime using

fn foo<'a, F>(f: Box<F>) -> Box<FnMut() -> i32 + 'a>
where F: Fn() -> i32 + 'a
    Box::new(move || f())