2022-02-06 16:43:51 +08:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// ANCHOR: here
|
|
|
|
// --snip--
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
let tx1 = tx.clone();
|
|
|
|
thread::spawn(move || {
|
|
|
|
let vals = vec![
|
|
|
|
String::from("hi"),
|
|
|
|
String::from("from"),
|
|
|
|
String::from("the"),
|
|
|
|
String::from("thread"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for val in vals {
|
|
|
|
tx1.send(val).unwrap();
|
|
|
|
thread::sleep(Duration::from_secs(1));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
let vals = vec![
|
|
|
|
String::from("more"),
|
|
|
|
String::from("messages"),
|
|
|
|
String::from("for"),
|
|
|
|
String::from("you"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for val in vals {
|
|
|
|
tx.send(val).unwrap();
|
|
|
|
thread::sleep(Duration::from_secs(1));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for received in rx {
|
2024-06-06 21:23:21 +08:00
|
|
|
println!("Got: {received}");
|
2022-02-06 16:43:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// --snip--
|
|
|
|
// ANCHOR_END: here
|
|
|
|
}
|