Warning
This post was published 61 days ago. The information described in this article may have changed.
Hello,
I am following the Rust book and have a question on the following code.
I found a question on the topic Iterating through Channel
but the answer does not explain what exactly happens.
How does the for loop iteration know when to wait for another input in rx
and when to exit the loop?
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("thread"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
println!("Got: {received}");
}
}
3 posts - 3 participants
🏷️ rust_feed