Card 1 / 6
Run a multiplication on a separate thread and return the result to the caller.
a |
b |
result |
|---|---|---|
6 |
7 |
42 |
0 |
5 |
0 |
-3 |
4 |
-12 |
thread::spawn(closure) starts a new thread running closure and immediately returns a JoinHandle. The closure needs move so it owns its captures rather than borrowing this stack frame, which may disappear first.
handle.join() blocks until that thread finishes and returns a Result — the Err case is a panicked thread — so .unwrap() hands you the closure's return value.
/// Spawn a thread that computes `a * b` and return its result.
fn product_in_thread(a: i32, b: i32) -> i32 {
let handle = thread::spawn(move || a * b);
handle.join().unwrap()
}Card 2 / 6
Hand a Vec to a new thread that takes ownership of it and computes the sum.
data |
result |
|---|---|
vec![1, 2, 3, 4] |
10 |
vec![] |
0 |
vec![-5, 5] |
0 |