0Pricing
Learn Rust Coding · Lesson

Joining and Collecting Results

Wait for threads and gather output.

Why Join Threads?

Spawning starts work, but you usually need its result or at least proof that it finished. Joining is how you wait.

In this lesson you will collect handles, join them, and gather the values they produce into a final answer.

join Returns a Result

join returns a Result. On success it holds the closure's return value; on a thread panic it holds an Err.

Calling unwrap gives the value directly, but propagates a panic if the thread failed.

use std::thread;

fn main() {
    let h = thread::spawn(|| 7 * 6);
    let value: i32 = h.join().unwrap();
    println!("answer = {}", value);
}

All lessons in this course

  1. Spawning Threads
  2. Moving Data into Threads
  3. Sharing with Arc and Mutex
  4. Joining and Collecting Results
← Back to Learn Rust Coding