有没有办法在生成的线程中调用闭包?

sbd*_*o85 2 multithreading rust

我有一个功能:

fn awesome_function<F>(v: Vec<u64>, f: F)
    where F: Fn(u64) -> String
{ /* ... */ }
Run Code Online (Sandbox Code Playgroud)

有没有办法调用在生成的线程中传递的函数?就像是:

...
thread::spawn(move || f(1));
...
Run Code Online (Sandbox Code Playgroud)

我尝试了几种不同的方法,但我得到了一个错误 error: the trait core::marker::Send is not implemented for the type F [E0277]

She*_*ter 5

绝对.你要做的主要是限制FSend:

use std::thread;

fn awesome_function<F>(v: Vec<u64>, f: F) -> String
    where F: Fn(u64) + Send + 'static
{
    thread::spawn(move || f(1));
    unimplemented!()
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

您还需要将其限制为'static,也需要thread::spawn.