Back to Comprehensive Rust

Unbounded Channels

src/concurrency/channels/unbounded.md

latest1.1 KB
Original Source
<!-- Copyright 2022 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

Unbounded Channels

You get an unbounded and asynchronous channel with mpsc::channel():

rust,editable
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let thread_id = thread::current().id();
        for i in 0..10 {
            tx.send(format!("Message {i}")).unwrap();
            println!("{thread_id:?}: sent Message {i}");
        }
        println!("{thread_id:?}: done");
    });
    thread::sleep(Duration::from_millis(100));

    for msg in rx {
        println!("Main: got {msg}");
    }
}
<details>
  • An unbounded channel will allocate as much space as is necessary to store pending messages. The send() method will not block the calling thread.
  • A call to send() will abort with an error (that is why it returns Result) if the channel is closed. A channel is closed when the receiver is dropped.
</details>