美文网首页RUST编程
012 Rust 异步编程,在 async 块中使用?

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

作者: 令狐壹冲 | 来源:发表于2020-07-19 11:14 被阅读0次

在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!");

}

相关文章

网友评论

    本文标题:012 Rust 异步编程,在 async 块中使用?

    本文链接:https://www.haomeiwen.com/subject/nxylkktx.html