0
0
Rustprogramming~10 mins

Threads overview in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Threads overview
Start main thread
Create new thread
New thread runs code
Main thread continues
Wait for new thread to finish (join)
Both threads done, program ends
This flow shows how the main thread starts, creates a new thread that runs concurrently, and then waits for it to finish before ending.
Execution Sample
Rust
use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        println!("Hello from new thread!");
    });
    handle.join().unwrap();
}
This Rust program creates a new thread that prints a message, then waits for it to finish before exiting.
Execution Table
StepActionThreadOutputState
1Start main functionMainMain thread running
2Create new thread with thread::spawnMainNew thread created, running concurrently
3New thread runs closureNewHello from new thread!New thread prints message
4Main thread continues after spawnMainMain thread waiting to join
5Main thread calls join()MainMain thread waits for new thread to finish
6New thread finishes executionNewNew thread ends
7join() returns, main thread resumesMainMain thread continues
8Main function endsMainProgram ends
💡 Program ends after main thread joins new thread and both finish execution
Variable Tracker
VariableStartAfter Step 2After Step 5Final
handleNoneThread handle createdThread joinedThread handle dropped
Key Moments - 2 Insights
Why does the main thread need to call join() on the new thread?
Without join(), the main thread might finish and exit the program before the new thread runs or finishes, as shown in step 5 of the execution_table where join() makes the main thread wait.
Does the new thread run before or after the main thread continues?
The new thread runs concurrently with the main thread. Step 3 shows the new thread printing while step 4 shows the main thread continuing, so they overlap in time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the new thread print its message?
AStep 5
BStep 2
CStep 3
DStep 7
💡 Hint
Check the Output column in execution_table rows for the print message
At which step does the main thread wait for the new thread to finish?
AStep 5
BStep 4
CStep 6
DStep 8
💡 Hint
Look for the join() call in the Action column of execution_table
If we remove handle.join(), what would happen to the program flow?
ANew thread never runs
BMain thread might finish before new thread
CMain thread waits anyway
DProgram crashes
💡 Hint
Refer to key_moments about why join() is needed to wait for new thread
Concept Snapshot
Threads overview in Rust:
- Use thread::spawn to create a new thread
- New thread runs closure concurrently
- Main thread continues immediately
- Use join() to wait for new thread to finish
- Program ends after all threads complete
Full Transcript
This example shows how Rust handles threads. The main thread starts and creates a new thread using thread::spawn. The new thread runs its code concurrently, printing a message. Meanwhile, the main thread continues and then calls join() to wait for the new thread to finish. After the new thread ends, join() returns and the main thread finishes, ending the program. This ensures both threads complete before exit.