mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-05-24 10:48:14 +08:00
34 lines
813 B
Rust
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
|
|
});
|
|
}
|