0
0
Rustprogramming~20 mins

Message passing concepts in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Message Passing 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 message passing code?

Consider this Rust program using channels for message passing. What will it print?

Rust
use std::sync::mpsc;
use std::thread;

fn main() {
    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        tx.send(42).unwrap();
    });
    let received = rx.recv().unwrap();
    println!("Received: {}", received);
}
ARuntime panic: channel disconnected
BReceived: 0
CCompile error: cannot move `tx` into closure
DReceived: 42
Attempts:
2 left
💡 Hint

Think about how the sender moves ownership of the transmitter into the thread and sends the value.

🧠 Conceptual
intermediate
1:30remaining
Which statement about Rust channels is true?

Choose the correct statement about Rust's std::sync::mpsc channels.

AMessages sent through channels must implement the Copy trait.
BThe receiver can be cloned to allow multiple receivers.
CThe sender can be cloned to allow multiple senders.
DChannels in Rust are synchronous by default.
Attempts:
2 left
💡 Hint

Think about which side of the channel supports cloning for multiple producers or consumers.

🔧 Debug
advanced
2:00remaining
What error does this Rust code produce?

Examine this Rust code snippet using channels. What error will it produce when compiled?

Rust
use std::sync::mpsc;

fn main() {
    let (tx, rx) = mpsc::channel();
    tx.send(10).unwrap();
    let val = rx.recv().unwrap();
    let val2 = rx.recv().unwrap();
    println!("{} {}", val, val2);
}
ADeadlock error at runtime
BRuntime panic: channel disconnected on second recv
COutput: 10 10
DCompile error: cannot borrow `rx` as mutable twice
Attempts:
2 left
💡 Hint

Consider what happens when you try to receive more messages than sent.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this Rust message passing code?

Identify the correct fix for the syntax error in this Rust code snippet:

let (tx, rx) = mpsc::channel();
thread::spawn(|| {
    tx.send(5).unwrap();
});
AAdd semicolon after channel creation: <code>let (tx, rx) = mpsc::channel();;</code>
BChange closure to move: <code>thread::spawn(move || { tx.send(5).unwrap(); });</code>
CDeclare tx as mutable: <code>let mut tx = mpsc::channel().0;</code>
DUse recv instead of send: <code>tx.recv(5).unwrap();</code>
Attempts:
2 left
💡 Hint

Think about ownership when moving variables into threads.

🚀 Application
expert
2:30remaining
How many messages are received in this Rust program?

Given this Rust program using multiple senders, how many messages will the receiver get?

Rust
use std::sync::mpsc;
use std::thread;

fn main() {
    let (tx, rx) = mpsc::channel();
    for i in 0..3 {
        let tx_clone = tx.clone();
        thread::spawn(move || {
            tx_clone.send(i).unwrap();
        });
    }
    drop(tx);
    let mut count = 0;
    while let Ok(_) = rx.recv() {
        count += 1;
    }
    println!("Received {} messages", count);
}
A3
B4
C0
D1
Attempts:
2 left
💡 Hint

Consider how many senders send messages and when the original sender is dropped.