Skip to content

Instantly share code, notes, and snippets.

@kimitoboku
Created December 8, 2015 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimitoboku/e35b88c48294615e46b1 to your computer and use it in GitHub Desktop.
Save kimitoboku/e35b88c48294615e46b1 to your computer and use it in GitHub Desktop.
rust spawn test
extern crate rand;
use std::time::Duration;
use std::thread;
use std::sync::mpsc::channel;
use rand::Rng;
fn main() {
let (tx,rx) = channel();
thread::spawn(move||{
let mut rand = rand::thread_rng();
for _ in 0..10{
let num :u64 = rand.gen::<u64>()%1000;
thread::sleep(Duration::from_millis(num));
tx.send(num).unwrap();
}
});
for _ in 0..10{
let meg = rx.recv().unwrap();
println!("{}",meg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment