Use thread::spawn to start a thread and handle.join() to wait for its completion.
Use thread::spawn to create a thread and handle.join() to wait for it to finish, ensuring resources are managed safely.
use std::thread;
fn main() {
let handle = thread::spawn(|| {
println!("Working in the spawned thread");
});
handle.join().unwrap();
println!("Thread finished");
}
The handle pattern lets you start a task in the background and keep a 'receipt' (the handle) to wait for it to finish. It matters because it prevents your main program from ending before background work is done. Think of it like ordering food at a restaurant: you get a ticket number (the handle) and wait at the counter until your order is ready (joining).