mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
2 months ago
|
extern crate trpl; // required for mdbook test
|
||
|
|
||
|
use std::time::Duration;
|
||
|
|
||
|
fn main() {
|
||
|
trpl::run(async {
|
||
|
let (tx, mut rx) = trpl::channel();
|
||
|
|
||
|
let tx1 = tx.clone();
|
||
|
let tx1_fut = async move {
|
||
|
let vals = vec![
|
||
|
String::from("hi"),
|
||
|
String::from("from"),
|
||
|
String::from("the"),
|
||
|
String::from("future"),
|
||
|
];
|
||
|
|
||
|
for val in vals {
|
||
|
tx1.send(val).unwrap();
|
||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
let rx_fut = async {
|
||
|
while let Some(value) = rx.recv().await {
|
||
|
println!("received '{value}'");
|
||
|
}
|
||
|
};
|
||
|
|
||
|
let tx_fut = async move {
|
||
|
let vals = vec![
|
||
|
String::from("more"),
|
||
|
String::from("messages"),
|
||
|
String::from("for"),
|
||
|
String::from("you"),
|
||
|
];
|
||
|
|
||
|
for val in vals {
|
||
|
tx.send(val).unwrap();
|
||
|
trpl::sleep(Duration::from_secs(1)).await;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// ANCHOR: here
|
||
|
let futures =
|
||
|
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
|
||
|
|
||
|
trpl::join_all(futures).await;
|
||
|
// ANCHOR_END: here
|
||
|
});
|
||
|
}
|