Created
March 21, 2022 13:56
-
-
Save jtriley-eth/5a73d9a65f7b95aaed768dbaa3d49a00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import `thread` module from std lib and `mpsc` module from `sync` from std lib | |
use std::thread; | |
use std::sync::mpsc; | |
// program entry point | |
fn main() { | |
// create sender and receiver with `channel()` | |
let (sender, receiver) = mpsc::channel(); | |
// spawn new thread, transfer ownership of `sender` to thread | |
// `move || {}` captures ownership of `sender` | |
thread::spawn(move || { | |
// declare a variable of type u8 | |
let my_variable: u8 = 42; | |
// send `my_variable` with the sender | |
// `unwrap()` panics if the send fails | |
sender.send(my_variable).unwrap(); | |
}); | |
// receive `my_variable` via `recv()` on the receiver | |
// name the variable `my_received` | |
let my_received = receiver.recv().unwrap(); | |
println!("Received {} from spawned thread", my_received); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment