【发布时间】:2022-07-30 19:29:32
【问题描述】:
tokio tutorial for select! 声明:
需要注意的是,要 .await 引用,被引用的值必须被固定或实现 Unpin。
确实,以下代码编译失败:
let fut = example(); // example is an async fn
(&mut fut).await;
带有以下错误消息:
error[E0277]: `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>` cannot be unpinned
... snip ...
within `impl futures::Future<Output = i32>`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>
... snip ...
note: consider using `Box::pin`
钉住未来解决问题:
let fut = example(); // example is an async fn
tokio::pin!(fut);
(&mut fut).await;
为什么需要固定未来以等待对它的引用?
【问题讨论】:
标签: rust async-await future