0
0
Rustprogramming~10 mins

Creating threads in Rust - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating threads
Start main thread
Call thread::spawn
Create new thread
Run closure in new thread
Main thread continues
Join new thread
Wait for thread to finish
Program ends
The main thread starts, creates a new thread with thread::spawn, runs code in it, then waits for it to finish with join.
Execution Sample
Rust
use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        println!("Hello from thread!");
    });
    handle.join().unwrap();
}
This code creates a new thread that prints a message, then the main thread waits for it to finish.
Execution Table
StepActionThreadOutputState
1Start main functionMainMain thread running
2Call thread::spawn with closureMainNew thread created, running closure
3New thread runs closureNew ThreadHello from thread!Closure executed
4Main thread continuesMainWaiting for new thread
5Call handle.join()MainWaiting for new thread to finish
6New thread finishesNew ThreadThread ends
7handle.join() returnsMainMain thread resumes
8Main function endsMainProgram ends
💡 Program ends after main thread waits for spawned thread to finish with join
Variable Tracker
VariableStartAfter thread::spawnAfter joinFinal
handleuninitializedJoinHandle createdjoin() called and returnedJoinHandle dropped
Key Moments - 3 Insights
Why do we need to call join() on the thread handle?
Calling join() makes the main thread wait for the spawned thread to finish, ensuring the program doesn't end early. See execution_table step 5 and 7.
Does the spawned thread run immediately after thread::spawn?
Yes, the new thread starts running the closure almost immediately after thread::spawn is called, as shown in execution_table step 3.
What happens if we don't call join()?
If join() is not called, the main thread may finish and end the program before the spawned thread completes, possibly cutting it off. This is why join() is important (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output printed by the new thread?
A"Hello from main!"
B"Hello from thread!"
CNo output
D"Thread finished"
💡 Hint
Check the Output column at step 3 in the execution_table
At which step does the main thread wait for the spawned thread to finish?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Look at the Action and State columns in execution_table steps 5 and 7
If we remove handle.join(), what is likely to happen?
AThe program may end before the spawned thread finishes
BThe main thread will wait anyway
CThe spawned thread will never run
DThe program will crash
💡 Hint
Refer to key_moments explanation about join() importance
Concept Snapshot
Creating threads in Rust:
- Use thread::spawn(|| { ... }) to start a new thread
- The closure runs in the new thread immediately
- thread::spawn returns a JoinHandle
- Call join() on JoinHandle to wait for thread completion
- Without join(), main may end before thread finishes
Full Transcript
This example shows how Rust creates threads using thread::spawn. The main thread starts and calls thread::spawn with a closure that prints a message. This creates a new thread that runs the closure immediately. The main thread continues and calls join() on the thread handle to wait for the spawned thread to finish. The program ends after the spawned thread completes. Calling join() is important to prevent the main thread from ending too soon. The variable handle holds the JoinHandle returned by thread::spawn and changes state as join() is called and returns.