trpl-zh-cn/listings/ch17-async-await/listing-17-12/src/main.rs
2024-10-20 23:44:05 +08:00

34 lines
813 B
Rust

extern crate trpl; // required for mdbook test
use std::time::Duration;
fn main() {
trpl::run(async {
// ANCHOR: with-move
let (tx, mut rx) = trpl::channel();
let tx_fut = async move {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("future"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
let rx_fut = async {
while let Some(value) = rx.recv().await {
eprintln!("received '{value}'");
}
};
trpl::join(tx_fut, rx_fut).await;
// ANCHOR_END: with-move
});
}