RosettaCodeData/Task/Concurrent-computing/Rust/concurrent-computing-3.rs

27 lines
523 B
Rust

use tokio::{sync::mpsc, task::JoinSet};
#[tokio::main]
async fn main() {
let words = vec!["Enjoy", "Rosetta", "Code"];
let mut set = JoinSet::new();
let (tx, mut rx) = mpsc::channel(words.len());
for word in words {
let tx = tx.clone();
set.spawn(async move {
tx.send(word).await.unwrap();
});
}
drop(tx);
tokio::spawn(async move {
set.join_all().await;
});
while let Some(word) = rx.recv().await {
println!("{}", word);
}
}