I have this code here: (Playground link)
use std::thread;
use std::sync::mpsc::channel;
fn run<T: Send>(task: fn() -> T) -> T {
    let (tx, rx) = channel();
    thread::spawn(move || {
        tx.send(task());
    });
    rx.recv().unwrap()
}
fn main() {
    let task = || 1 + 2;
    let result = run(task);
    println!("{}", result);
}
But I'm getting a lifetime error I can't figure out.
<anon>:6:5: 6:18 error: the parameter type `T` may not live long enough [E0310]
<anon>:6     thread::spawn(move || {
             ^~~~~~~~~~~~~
<anon>:6:5: 6:18 help: consider adding an explicit lifetime bound `T: 'static`...
<anon>:6:5: 6:18 note: ...so that captured variable `tx` does not outlive the enclosing closure
<anon>:6     thread::spawn(move || {
             ^~~~~~~~~~~~~
<anon>:15:22: 15:26 error: mismatched types:
 expected `fn() -> _`,
    found `[closure <anon>:13:16: 13:24]`
(expected fn pointer,
    found closure) [E0308]
<anon>:15     let result = run(task);
                               ^~~~
Any suggestions? Thanks!
                        
The error message suggests adding a
'staticbound to the type parameterT. If you do this, it will get rid of the first error:The
'staticbound is needed to guarantee that the value returned bytaskcan outlive the function wheretaskruns. Read more about the'staticlifetime.The second error is that you are passing a closure, while
runexpects a function pointer. One way to fix this is by changingtaskfrom a closure to a fn:Here's the complete working code: