< 返回版块

243011068 发表于 2020-01-05 11:25

Tags:async_std,futures,mpsc,select!

原谅我不知道怎么起标题,直接上代码

use async_std::io;
use async_std::net::TcpStream;
use async_std::prelude::*;
use async_std::task;

use futures::{channel::mpsc, select, FutureExt, SinkExt};
use std::time::Duration;
use std::collections::HashMap;

type Sender<T> = mpsc::UnboundedSender<T>;
type Receiver<T> = mpsc::UnboundedReceiver<T>;

fn main() {
    task::block_on(async {
        let mut hash_map = HashMap::new();
        let (sender, receiver) = mpsc::unbounded::<String>();
        hash_map.insert(1, receiver);
        select! {
            for (_,r) in hash_map.iter() {
                    msg = r.next().fuse() => match msg {
                    Some(msg) => { println!("11");},
                    None => {},
                }
            }
        };
    })
}

目的是要在select!宏里用map迭代receiver

评论区

写评论
guoxbin 2020-01-08 09:15

select!是编译时展开,for是运行时,所以没法用select!

可以用for把所有recv的future放到一个Vec里,然后用select_all

1 共 1 条评论, 1 页