Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 新线程中的异步函数_Rust - Fatal编程技术网

Rust 新线程中的异步函数

Rust 新线程中的异步函数,rust,Rust,我试图在新线程中调用异步函数 async fn init(){ println!("Running"); } fn load(){ thread::spawn(init); //Or async closure thread::spawn(async|| { init().await; }); } 但我不能做到。感谢您的帮助。std::thread不是未来任务的执行者。您需要一个执行器来运行未来,或者您需要在不同的线程上手动轮询它(这

我试图在新线程中调用异步函数

async fn init(){
    println!("Running");
}

fn load(){
    thread::spawn(init);

    //Or async closure
    thread::spawn(async|| {
        init().await;
    });
}

但我不能做到。感谢您的帮助。

std::thread
不是
未来任务的执行者。您需要一个执行器来运行未来,或者您需要在不同的线程上手动轮询它(这可以被视为实现了执行器^^)。请检查这个简单的示例:,它使用
block\u on
在轮询未来时阻塞封闭线程。您也可以在tokio板条箱中检查运行时功能。@ÖmerErden谢谢!