Join and panic handling
Quick answer
A join handle lets the caller wait for a worker and receive its return value or an error indicating a panic when unwinding is enabled.
Distinguish panic from ordinary errors
JoinHandle::join consumes the handle. Under panic unwinding, the outer Result reports a panic payload; the worker's own Result, if present, represents application failure. Dropping the handle detaches the thread rather than cancelling it. A panic-abort build may terminate the process instead of returning a panic payload.
fn main() { let task = std::thread::spawn(|| "no".parse::<u32>()); let result = task.join().expect("worker panicked"); println!("parse failed: {}", result.is_err());}Expected stdout (not executed):
parse failed: trueThe worker completed normally even though parsing failed.
fn main() { let task = std::thread::spawn(|| -> () { panic!("demo") }); println!("panicked: {}", task.join().is_err());}Expected stdout (not executed):
panicked: trueAssumes unwind mode; a panic hook may also print to stderr.
Common mistake
Do not ignore the result of join in a service that must account for every job.
Try it yourself
Make a worker return Result<u32, &str> and handle both levels of Result.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Join and panic handling — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A worker returns Result<u32, E>. join succeeds but the returned Result is Err. What failed?
Join and panic handling — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let task = std::thread::spawn(|| "no".parse::<u32>()); let result = task.join().expect("worker panicked"); println!("parse failed: {}", result.is_err()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Join and panic handling — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let task = std::thread::spawn(|| -> () { panic!("demo") }); println!("panicked: {}", task.join().is_err()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.