Skip to content

Instantly share code, notes, and snippets.

Created May 24, 2017 03:08
Show Gist options
  • Save anonymous/aa82a4b833ba678de8a4727b9e38d8e3 to your computer and use it in GitHub Desktop.
Save anonymous/aa82a4b833ba678de8a4727b9e38d8e3 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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