原谅我不知道怎么起标题,直接上代码
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
1
共 1 条评论, 1 页
评论区
写评论select!是编译时展开,for是运行时,所以没法用select!
可以用for把所有recv的future放到一个Vec里,然后用select_all