012 Rust 异步编程,在 async 块中使用?

在Rust异步编程中能否像在同步编程中一样使用问号呢?我们来试试。

示例

  • 源码
[dependencies]
futures = "0.3"
  • 配置文件
use futures;

async fn foo() -> Result<(), String>{
    "foo";
    Ok(())
}

async fn func() -> Result<(), String>{

    let fut = async {
        foo().await?;
        Ok::<(), String>(()) // <- note the explicit type annotation here
        //Ok(()) // <- note the explicit type annotation here
    };

    fut.await
}

fn main() {
    let _ = futures::executor::block_on(func());
    println!("Hello, world!");

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
令狐一冲
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
文章
255
粉丝
120
喜欢
308
收藏
128
排名:335
访问:2.8 万
私信
所有博文
社区赞助商