Skip to content

Instantly share code, notes, and snippets.

@mandel59
Created April 28, 2014 07:27
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 mandel59/11364193 to your computer and use it in GitHub Desktop.
Save mandel59/11364193 to your computer and use it in GitHub Desktop.
FizzBuzz with green threads
extern crate green;
extern crate rustuv;
#[start]
fn start(argc: int, argv: **u8) -> int {
green::start(argc, argv, rustuv::event_loop, 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