expression
Consume a receiver as an iterator
IntoIterator for Receiver owns the endpoint and reads until disconnection and draining. An owning loop prevents reusing the consumed receiver afterward. Expected stdout (not executed): 9 Common mistake: Trying to use rx after moving it into the loop.
Formal Syntax & Structure
rust syntaxrust
for message in receiver { ... }Working Example
Example Snippetrust
fn main() { use std::sync::mpsc; let (tx, rx) = mpsc::channel(); tx.send(2).unwrap(); tx.send(7).unwrap(); drop(tx); let mut total = 0; for value in rx { total += value; } println!("{total}");}