RUST编程

010 Rust 异步编程,使用 select 宏的条件

2020-07-19  本文已影响0人  令狐壹冲

使用select宏

使用流的示例:

use futures::{
    stream::{Stream, StreamExt, FusedStream},
    select,
};

async fn add_two_streams(
    mut s1: impl Stream<Item = u8> + FusedStream + Unpin,
    mut s2: impl Stream<Item = u8> + FusedStream + Unpin,
) -> u8 {
    let mut total = 0;

    loop {
        let item = select! {
            x = s1.next() => x,
            x = s2.next() => x,
            complete => break,
        };
        if let Some(next_num) = item {
            total += next_num;
        }
    }

    total
}

其它

上一篇下一篇

猜你喜欢

热点阅读