use futures; // 0.3.28
use futures::future;
use futures::executor::block_on;
use futures::channel::mpsc::unbounded;
use futures::pin_mut;
use futures::SinkExt;
use futures::StreamExt;
use futures::future::Either;
fn main() {
let (mut tx, rx) = unbounded::[i]();
let x = async {
for i in 0..100 {
tx.send(i).await.expect("send failed");
}
};
let y = rx.collect::>();
pin_mut!(x, y);
block_on(async {
match future::select(x, y).await {
Either::Left((_, y)) => {
let res = y.await;
println!("{:?}", res);
},
Either::Right((res, _)) => {
println!("{:?}", res);
}
}
});
}
而这一段代码可以运行,不会卡住
use futures; // 0.3.28
use futures::future;
use futures::executor::block_on;
use futures::channel::mpsc::unbounded;
use futures::pin_mut;
use futures::SinkExt;
use futures::StreamExt;
use futures::future::Either;
fn main() {
let (mut tx, rx) = unbounded::[i]();
let x = async move {
for i in 0..100 {
tx.send(i).await.expect("send failed");
}
};
let y = rx.collect::>();
pin_mut!(x, y);
block_on(async {
match future::select(x, y).await {
Either::Left((_, y)) => {
let res = y.await;
println!("{:?}", res);
},
Either::Right((res, _)) => {
println!("{:?}", res);
}
}
});
}
rust playgroud