Channels and bounded queues
Quick answer
Channels transfer messages between execution contexts. A bounded channel also limits buffered messages, providing a way to slow producers when consumers fall behind.
Send, receive and close
std::sync::mpsc::channel is an unbounded multi-producer single-consumer channel; sync_channel(n) has bounded capacity and blocking send. try_send reports Full or Disconnected without blocking. The receiver can finish iterating after all senders are dropped and queued messages are drained. Avoid blocking on a full queue if the same thread must drain it.
use std::sync::mpsc;fn main() { let (tx, rx) = mpsc::channel(); let worker = std::thread::spawn(move || { for n in [2, 3, 5] { tx.send(n).unwrap(); } }); println!("{}", rx.iter().sum::<i32>()); worker.join().unwrap();}Expected stdout (not executed):
10Dropping the last sender at worker exit ends iteration.
use std::sync::mpsc::{sync_channel, TrySendError};fn main() { let (tx, rx) = sync_channel(1); tx.send(10).unwrap(); assert!(matches!(tx.try_send(20), Err(TrySendError::Full(20)))); println!("{}", rx.recv().unwrap());}Expected stdout (not executed):
10The second message does not fit until the consumer removes one.
Common mistake
Do not keep unused sender clones alive while waiting for receiver iteration to end.
Try it yourself
Change the capacity to two and verify the second try_send succeeds.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Channels and bounded queues — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A receiver should finish iteration after processing every queued message. What must senders do?
Channels and bounded queues — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::mpsc; fn main() { let (tx, rx) = mpsc::channel(); let worker = std::thread::spawn(move || { for n in [2, 3, 5] { tx.send(n).unwrap(); } }); println!("{}", rx.iter().sum::<i32>()); worker.join().unwrap(); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Channels and bounded queues — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::mpsc::{sync_channel, TrySendError}; fn main() { let (tx, rx) = sync_channel(1); tx.send(10).unwrap(); assert!(matches!(tx.try_send(20), Err(TrySendError::Full(20)))); println!("{}", rx.recv().unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.