"Do not communicate by sharing memory..."
Wait! Rust follows the famous Go proverb: "Do not communicate by sharing memory; instead, share memory by communicating."
The most common way to do this in Rust is by using Channels. A channel has two parts: a Transmitter (Sender) and a Receiver.
Creating a Channel
We create a channel using the
mpsc::channel() function. mpsc stands for Multi-Producer, Single-Consumer. This means you can have many threads sending messages to a single thread that receives them.rust
Ownership and Channels
Wait! When you send a value through a channel, ownership is moved. Once you call
tx.send(val), you can no longer use val in the sending thread. This prevents data races!Multi-Producer Logic
Because the transmitter is "Multi-Producer," you can clone it to give multiple threads the ability to send messages to the same receiver.
rust
Receiving Multiple Messages
You can treat the receiver like an iterator to process messages as they arrive. The loop will finish when all transmitters are dropped.
rust
Summary
| Term | Syntax | Description |
|---|---|---|
| mpsc | std::sync::mpsc | Multi-Producer Single-Consumer |
| Sender | tx.send(val) | Putting data into the channel |
| Receiver | rx.recv() | Blocking wait for data |
| try_recv | rx.try_recv() | Non-blocking check for data |
| Clone | tx.clone() | Creating additional transmitters |
| Move | Transfer | Data sent to a channel is MOVED |
| Safe | Sync | Channels ensure thread-safe communication |
| Best For | Task pipelines, work distribution, event handling | |
| Bound | sync_channel | A channel with a fixed capacity (backpressure!) |
| Logic | The channel is closed when all Senders are dropped | |
| Safety | Prevents "Double use" and data races via ownership | |
| Standard | mpsc is built into the Rust standard library |