Futures低调发布0.3.1正式版,全面支持async/await

2019-11-14 15:23:35 浏览数 (2)

无需Tokio/Async-std,自带Runtime。代码库的参考文档比较完善,只是缺少应用指南。下面是一段测试代码:

代码语言:javascript复制
use futures::executor::block_on;
use futures::future::{join_all, pending, select_all};
use futures::prelude::*;

async fn a(a: i32, b: i32) -> i32 {
    a   b
}

async fn b(a: i32, b: i32) -> i32 {
    a - b
}

async fn c(a: i32, b: i32) -> i32 {
    pending().await
}

fn main() {
    let r = block_on(async {
        println!("{}", a(1, 2).await);
        println!("{}", b(1, 2).await);

        async fn foo(i: u32) -> u32 {
            i * 10
        }

        let futures = vec![foo(1), foo(2), foo(3)];
        let all_finished = join_all(futures).await;
        println!("{:?}", all_finished);

        // let first_finished = select_all(futures).await;
        // println!("{:?}", first_finished);

        "block_on result"
    });
    println!("{}", r)
}

0 人点赞