Created
April 28, 2014 07:27
-
-
Save mandel59/11364171 to your computer and use it in GitHub Desktop.
FizzBuzz with native threads
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
extern crate native; | |
#[start] | |
fn start(argc: int, argv: **u8) -> int { | |
native::start(argc, argv, main) | |
} | |
fn main() { | |
let n = 10000; | |
let (tx, rx) = channel::<(int, std::str::MaybeOwned<'static>)>(); | |
for i in range(0, n) { | |
let tx = tx.clone(); | |
spawn(proc() { | |
let i = i + 1; | |
let b = match (i % 3, i % 5) { | |
(0, 0) => "FizzBuzz".into_maybe_owned(), | |
(0, _) => "Fizz".into_maybe_owned(), | |
(_, 0) => "Buzz".into_maybe_owned(), | |
_ => i.to_str().into_maybe_owned() | |
}; | |
tx.send((i, b)); | |
}); | |
} | |
let mut res = ~[]; | |
for _ in range(0, n) { | |
res.push(rx.recv()); | |
} | |
res.sort_by(|t, u| t.ref0().cmp(u.ref0())); | |
for t in res.iter() { | |
println!("{}", t.ref1().as_slice()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment