How to Use Channel Patterns (MPSC, MPMC, Broadcast) in Rust

Use std::sync::mpsc for single-consumer, crossbeam-channel for multi-consumer, and std::sync::broadcast for one-to-many messaging in Rust.

Use std::sync::mpsc for single-consumer channels, crossbeam-channel for multi-consumer (MPMC), and std::sync::broadcast for one-to-many broadcasting.

use std::sync::mpsc;
use std::thread;

fn main() {
    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        tx.send(String::from("hello")).unwrap();
    });
    println!("Received: {}", rx.recv().unwrap());
}

For MPMC, add crossbeam-channel to Cargo.toml and use crossbeam_channel::unbounded(). For broadcast, use std::sync::broadcast::channel() and clone the sender to multiple threads.