0
0
Rustprogramming~10 mins

Message passing concepts in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Message passing concepts
Create Channel
Sender sends message
Message in Channel Buffer
Receiver receives message
Process message
Repeat or Close Channel
Message passing in Rust uses channels where a sender sends messages to a receiver through a buffer, enabling safe communication between threads.
Execution Sample
Rust
use std::sync::mpsc;
fn main() {
    let (tx, rx) = mpsc::channel();
    tx.send(42).unwrap();
    let val = rx.recv().unwrap();
    println!("Received: {}", val);
}
This code creates a channel, sends the number 42 from sender to receiver, then prints the received value.
Execution Table
StepActionChannel StateSender StateReceiver StateOutput
1Create channelEmpty bufferSender readyReceiver ready
2Sender sends 42Buffer contains 42Sender readyReceiver ready
3Receiver calls recv()Buffer empty after recvSender readyReceiver received 42
4Print received valueBuffer emptySender readyReceiver received 42Received: 42
5End of exampleBuffer emptySender readyReceiver received 42Program ends
💡 Program ends after message is received and printed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
tx (Sender)CreatedReady to sendReady to sendReady
rx (Receiver)CreatedReady to receiveReceived 42Received 42
Channel BufferEmptyContains 42EmptyEmpty
valNoneNone4242
Key Moments - 3 Insights
Why does the receiver block at recv() until a message arrives?
Because recv() waits for a message to be available in the channel buffer, as shown in step 3 of the execution_table where the receiver waits and then receives 42.
What happens if the sender sends multiple messages before the receiver calls recv()?
Messages accumulate in the channel buffer until the receiver calls recv(), similar to step 2 where the buffer holds the message until received.
Can the sender send messages after the receiver has dropped?
No, sending will fail because the channel is closed when the receiver is dropped, which would cause send() to return an error (not shown in this simple example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the state of the channel buffer?
AEmpty
BContains 42
CContains multiple messages
DClosed
💡 Hint
Check the 'Channel State' column at step 2 in the execution_table.
At which step does the receiver actually get the message?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Receiver State' column to see when it changes to 'Received 42'.
If the sender sends two messages before the receiver calls recv(), how would the channel buffer state change at step 2?
ABuffer would be empty
BBuffer would contain one message
CBuffer would contain two messages
DBuffer would be closed
💡 Hint
Recall that messages accumulate in the buffer until received, as explained in key_moments.
Concept Snapshot
Message passing in Rust uses channels with a sender and receiver.
Sender sends messages into a buffer.
Receiver waits and receives messages from the buffer.
recv() blocks until a message is available.
Channels enable safe communication between threads.
Full Transcript
This example shows how Rust's message passing works using channels. First, a channel is created with a sender and receiver. The sender sends the number 42 into the channel buffer. The receiver waits at recv() until the message arrives, then receives it. Finally, the program prints the received value. The channel buffer holds messages until the receiver takes them. This mechanism allows threads to communicate safely by passing messages instead of sharing memory directly.