Why rustc has a problem with thread using parent's data despite join?

⚓ rust    📅 2025-05-30    👤 surdeus    👁️ 3      

surdeus

I don't understand why rust compiler would have a problem with Listing 16-3 in the rustbook: Using Threads to Run Code Simultaneously - The Rust Programming Language

Pasting that code snippet below for convenience:

use std::thread;

fn main() {
    let v = vec![1, 2, 3];

    let handle = thread::spawn(|| {
        println!("Here's a vector: {v:?}");
    });

    handle.join().unwrap();
}

The main thread won't exit without the spawned thread being joined anyway. Even if the OS schedules that thread for later, the main thread is still blocked on the call to join.

Any ideas?

11 posts - 6 participants

Read full topic

🏷️ rust_feed