How to Share Data Between Threads in Rust

Share data between Rust threads safely using std::sync::mpsc channels to move ownership.

Use std::sync::mpsc channels to send data between threads safely by moving ownership across thread boundaries.

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

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