让我们考虑以下示例:
主
use futures::executor::block_on;
use futures::future::{FutureExt, TryFutureExt};
async fn fut1() -> Result<String, u32> {
Ok("ok".to_string())
}
fn main() {
println!("Hello, world!");
match block_on(fut1().and_then(|x| async move { Ok(format!("{} is \"ok\"", x)) })) {
Ok(s) => println!("{}", s),
Err(u) => println!("{}", u)
};
}
货代
[dependencies]
futures = "^0.3"
我要问的是表达式|x| async move {}
而不是async move |x| {}
。后者更为明显,但是会遇到编译错误:
error[E0658]: async closures are unstable
然后我想知道async move || {}
和之间有什么区别|| async move {}
。它们似乎都是使用move
关键字的闭包。
$ rustc --version
rustc 1.39.0 (4560ea788 2019-11-04)