How to pass ownership through channels

Pass ownership through Rust channels by sending values via mpsc::channel, which moves data from sender to receiver.

You pass ownership through channels by sending the value itself, which moves it from the sender to the receiver. Use std::sync::mpsc to create a channel, send the value with send(), and receive it with recv(). The sender gives up ownership upon sending, and the receiver takes ownership upon receiving.

use std::sync::mpsc;

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