这tokio tutorial for select!
个州:
需要注意的是,要等待引用,被引用的值必须被固定或实现取消固定.
实际上,以下代码无法编译:
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`
锁定future 解决了问题:
let fut = example(); // example is an async fn
tokio::pin!(fut);
(&mut fut).await;
为什么有必要为了等待future 的引用而将future 固定下来?