I have the following pieces of code:
use std::thread;
use std::sync::mpsc;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let vals = vec![
String::from("The"),
String::from("Quick"),
String::from("Brown"),
String::from("Fox"),
String::from("Jumped"),
String::from("Over"),
String::from("The"),
String::from("Lazy"),
String::from("Dog."),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
print!("{} ", received);
}
println!("");
}
and
use std::thread;
use std::sync::mpsc;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let vals = vec![
String::from("The"),
String::from("Quick"),
String::from("Brown"),
String::from("Fox"),
String::from("Jumped"),
String::from("Over"),
String::from("The"),
String::from("Lazy"),
String::from("Dog."),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
println!("{} ", received);
}
println!("");
}
If I run the first code, about 10 seconds elapse before I see any output but the second behaves as expected with a line being output to stdout every second. I would appreciate an explanation to why the above happens as outlined.