-
-
Save sliminality/57404a1cff7705fd2121cd67c4c5549a to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains hidden or 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
use std::sync::{Arc, Mutex}; | |
use std::thread; | |
use std::sync::mpsc; | |
fn main() { | |
let v = vec![1, 2]; | |
println!("{:?}", v.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(" ")); | |
let mut iter = (0..4).zip((0..6)); | |
for (x, y) in iter { | |
println!("{:?}, {:?}", x, y); | |
} | |
let data = Arc::new(Mutex::new(0)); | |
// `tx` is the "transmitter" or "sender". | |
// `rx` is the "receiver". | |
let (tx, rx) = mpsc::channel(); | |
for _ in 0..10 { | |
let (data, tx) = (data.clone(), tx.clone()); | |
thread::spawn(move || { | |
let mut data = data.lock().unwrap(); | |
*data += 1; | |
tx.send(()).unwrap(); | |
}); | |
} | |
for _ in 0..10 { | |
rx.recv().unwrap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment