0
0
Rustprogramming~20 mins

Shared state overview in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Shared State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Rust code using Arc and Mutex?

Consider this Rust program that shares state between threads using Arc and Mutex. What will it print?

Rust
use std::sync::{Arc, Mutex};
use std::thread;

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

    for _ in 0..5 {
        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.join().unwrap();
    }

    println!("Counter: {}", *counter.lock().unwrap());
}
ACompilation error due to missing Send trait
BCounter: 5
CCounter: 1
DCounter: 0
Attempts:
2 left
💡 Hint

Think about how Arc and Mutex work together to allow safe shared access.

🧠 Conceptual
intermediate
1:30remaining
Which Rust type is used to share mutable state safely across threads?

In Rust, which combination of types is commonly used to share mutable state safely across multiple threads?

AArc and Mutex
BRc and RefCell
CBox and Cell
DVec and String
Attempts:
2 left
💡 Hint

One type provides thread-safe reference counting, the other provides mutual exclusion.

🔧 Debug
advanced
2:30remaining
What error does this Rust code produce when sharing state without synchronization?

What error will this Rust code produce?

use std::sync::Arc;
use std::thread;

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

    for _ in 0..5 {
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            // Trying to increment counter directly
            *counter += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Counter: {}", *counter);
}
ANo error, prints Counter: 5
Berror[E0277]: the trait `Send` is not implemented for `i32`
Cerror[E0382]: use of moved value `counter`
Derror[E0596]: cannot borrow data in an Arc as mutable
Attempts:
2 left
💡 Hint

Think about mutability and thread safety with Arc.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a thread-safe shared counter in Rust?

Choose the Rust code snippet that correctly creates a thread-safe shared counter initialized to zero.

Alet counter = Arc::new(Mutex::new(0));
Blet counter = Rc::new(RefCell::new(0));
Clet counter = Box::new(0);
Dlet counter = Mutex::new(Arc::new(0));
Attempts:
2 left
💡 Hint

Remember which types are thread-safe and which are not.

🚀 Application
expert
3:00remaining
How many times is the shared counter incremented in this Rust program?

Given this Rust program, how many times will the shared counter be incremented?

use std::sync::{Arc, Mutex};
use std::thread;

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

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

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Counter: {}", *counter.lock().unwrap());
}
A1
B10
C100
DCompilation error due to deadlock
Attempts:
2 left
💡 Hint

Count how many threads and increments each thread performs.