Demonstrating Buffered Channels in Rust

Rust Error Propagation: Exercise-8 with Solution

Write a Rust program to demonstrate the use of buffered channels. Create a channel with a buffer size of 3 and send 5 messages through the channel. Ensure that sending blocks when the buffer is full and resumes when space becomes available.

Sample Solution:

Rust Code:

use std::sync::mpsc; // Import the mpsc module for channels
use std::thread; // Import the thread module for multi-threading

const BUFFER_SIZE: usize = 3; // Define the buffer size for the channel
const NUM_MESSAGES: usize = 5; // Define the number of messages to send

fn main() {
    // Create a buffered channel with a capacity of 3
    let (sender, receiver) = mpsc::sync_channel(BUFFER_SIZE);

    // Spawn a thread to send messages
    let sender_thread = thread::spawn(move || {
        // Send 5 messages through the channel
        for i in 0..NUM_MESSAGES {
            sender.send(i).unwrap(); // Send a message
            println!("Sent: {}", i); // Print the sent message

    // Spawn a thread to receive messages
    let receiver_thread = thread::spawn(move || {
        // Receive 5 messages from the channel
        for _ in 0..NUM_MESSAGES {
            let message = receiver.recv().unwrap(); // Receive a message
            println!("Received: {}", message); // Print the received message

    // Wait for sender and receiver threads to finish
    sender_thread.join().unwrap(); // Wait for the sender thread to finish
    receiver_thread.join().unwrap(); // Wait for the receiver thread to finish


Sent: 0
Sent: 1
Sent: 2
Sent: 3
Received: 0
Received: 1
Received: 2
Received: 3
Sent: 4
Received: 4


In the exercise above,

  • Import modules: The program imports 'std::sync::mpsc' for channels and 'std::thread' for thread management.
  • Define constants: It sets constants for the buffer size ('BUFFER_SIZE') and the number of messages to send ('NUM_MESSAGES').
  • Main function:
    • Channel creation: It creates a buffered channel with a capacity of 3 using 'mpsc::sync_channel'.
    • Spawn sender thread: It spawns a thread to send messages through the channel. The sender thread sends 5 messages sequentially.
    • Spawn receiver thread: It spawns another thread to receive messages from the channel. The receiver thread receives 5 messages sequentially.
    • Wait for threads to finish: It waits for both sender and receiver threads to finish execution using join().
  • Sender thread: Sends 5 messages through the channel. When the buffer is full, sending blocks until space becomes available.
  • Receiver thread: Receives 5 messages from the channel. As messages are sent and buffer space becomes available, receiving resumes.

Rust Code Editor:

Previous: Implementing a Message Broadcast System in Rust.
Next: Implementing Request-Response Pattern with Channels in Rust.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.