有一段代码不是很理解,为什么第一段代码会卡住,不会出结果?
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::<i32>();
let x = async {
for i in 0..100 {
tx.send(i).await.expect("send failed");
}
};
let y = rx.collect::<Vec<_>>();
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::<i32>();
let x = async move {
for i in 0..100 {
tx.send(i).await.expect("send failed");
}
};
let y = rx.collect::<Vec<_>>();
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);
}
}
});
}