w3resource

Rust Message Passing Exercises with Solutions and Explanations

Rust Message Passing (channels) Exercises [9 exercises with solution and Explanation]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a Rust program that creates two threads and uses a channel to send a message from one thread to another.

Click me to see the solution

2. Write a Rust program that extends the previous program to send multiple messages through the channel and print them in the receiving thread.

Click me to see the solution

3. Write a Rust program that creates multiple sender threads and a single receiver thread. Each sender thread sends a message through the channel, and the receiver thread prints the messages.

Click me to see the solution

4. Write a Rust program that implements a producer-consumer pattern in Rust using channels. Create one producer thread and multiple consumer threads. The producer thread generates data and sends it through the channel, and the consumer threads receive and process the data.

Click me to see the solution

5. Write a Rust program to simulate a message passing network, where each node communicates with its neighboring nodes through channels. Implement message routing and forwarding between nodes.

Click me to see the solution

6. Write a Rust program to implement the use of synchronous channels. Create a channel with synchronous communication and send messages between threads, ensuring that messages are received before continuing.

Click me to see the solution

7. Write a Rust program to implement a message broadcast system using channels. Create one sender thread and multiple receiver threads. When the sender sends a message through the channel, all receiver threads should receive and print the message.

Click me to see the solution

8. 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.

Click me to see the solution

9. Write a Rust program to implement a request-response pattern using channels. Create one thread to handle requests and another thread to handle responses. When a request is received, the handler thread processes it and sends a response back through the channel.

Click me to see the solution

Rust Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.