假设有一个异步函数及其对应的 Future:
async fn foo() -> Result<i32, &'static str> {
// ...
}
let my_future = foo();
使用只有 .await 和使用 tokio::spawn().await 有什么区别?
// like this...
let result1 = my_future.await;
// ... and like this
let result2 = tokio::spawn(my_future).await;
my_future.await
需要在其后紧跟的任何代码运行之前完成。生成新任务将继续当前任务,同时在新任务中运行未来。 - Peter Hall