0
0
Rustprogramming~10 mins

Threads overview in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new thread that prints "Hello from thread!".

Rust
use std::thread;

fn main() {
    let handle = thread::[1](|| {
        println!("Hello from thread!");
    });
    handle.join().unwrap();
}
Drag options to blanks, or click blank then click option'
Acreate
Bstart
Crun
Dspawn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'run' which are not Rust thread functions.
2fill in blank
medium

Complete the code to wait for the thread to finish execution.

Rust
use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        println!("Thread is running");
    });
    handle.[1]().unwrap();
}
Drag options to blanks, or click blank then click option'
Ajoin
Bfinish
Cwait
Dcomplete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'wait' or 'finish' which are not valid methods on thread handles.
3fill in blank
hard

Fix the error in the code to correctly share data between threads using Arc.

Rust
use std::sync::[1];
use std::sync::Arc;
use std::thread;

fn main() {
    let counter = Arc::new([2]::new(0));
    let counter = Arc::clone(&counter);

    let handle = thread::spawn(move || {
        let mut num = counter.lock().unwrap();
        *num += 1;
    });

    handle.join().unwrap();
    println!("Counter: {}", *counter.lock().unwrap());
}
Drag options to blanks, or click blank then click option'
AMutex
BRwLock
CCell
DRefCell
Attempts:
3 left
💡 Hint
Common Mistakes
Using Cell or RefCell which are not thread-safe.
4fill in blank
hard

Fill both blanks to create a thread that increments a shared counter safely.

Rust
use std::sync::{Arc, [1];
use std::thread;

fn main() {
    let counter = Arc::new([2]::new(0));
    let counter_clone = Arc::clone(&counter);

    let handle = thread::spawn(move || {
        let mut num = counter_clone.lock().unwrap();
        *num += 1;
    });

    handle.join().unwrap();
    println!("Counter: {}", *counter.lock().unwrap());
}
Drag options to blanks, or click blank then click option'
AMutex
BRwLock
CCell
DRefCell
Attempts:
3 left
💡 Hint
Common Mistakes
Using RwLock or Cell which are not appropriate here.
5fill in blank
hard

Fill all three blanks to create multiple threads that increment a shared counter safely.

Rust
use std::sync::{Arc, [1];
use std::thread;

fn main() {
    let counter = Arc::new([2]::new(0));
    let mut handles = vec![];

    for _ in 0..10 {
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            let mut num = counter.lock().unwrap();
            *num += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.[3]().unwrap();
    }

    println!("Final counter: {}", *counter.lock().unwrap());
}
Drag options to blanks, or click blank then click option'
AMutex
Cjoin
Dlock
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lock' instead of 'join' to wait for threads.