Iterating through Channel

Hello, I ran through this code snippet in the Rust Book:

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);
    }
}

My question is, does the main thread waits for all data to be transmitted before looping over rx? Because the outputs shows no repetition.

Thanks!

Main thread loops over rx by waiting for each next piece of data to arrive. That is, it will almost immediately pull "hi", then wait for a second until thread pushes "from".

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.