2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2016-02-11 05:34:41 -06:00
|
|
|
// ignore-emscripten no threads support
|
|
|
|
|
2015-03-30 13:00:05 -05:00
|
|
|
use std::thread;
|
2014-12-23 13:53:35 -06:00
|
|
|
use std::sync::mpsc::{channel, Receiver};
|
2014-12-22 11:04:23 -06:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn periodical(n: isize) -> Receiver<bool> {
|
2014-04-27 22:27:20 -05:00
|
|
|
let (chan, port) = channel();
|
2015-03-30 13:00:05 -05:00
|
|
|
thread::spawn(move|| {
|
2014-04-27 22:27:20 -05:00
|
|
|
loop {
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 1..n {
|
2014-12-23 13:53:35 -06:00
|
|
|
match chan.send(false) {
|
2014-04-27 22:27:20 -05:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(..) => break,
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 13:53:35 -06:00
|
|
|
match chan.send(true) {
|
2014-04-27 22:27:20 -05:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(..) => break
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 23:59:45 -06:00
|
|
|
});
|
2014-04-27 22:27:20 -05:00
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn integers() -> Receiver<isize> {
|
2014-04-27 22:27:20 -05:00
|
|
|
let (chan, port) = channel();
|
2015-03-30 13:00:05 -05:00
|
|
|
thread::spawn(move|| {
|
2014-04-27 22:27:20 -05:00
|
|
|
let mut i = 1;
|
|
|
|
loop {
|
2014-12-23 13:53:35 -06:00
|
|
|
match chan.send(i) {
|
2014-04-27 22:27:20 -05:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(..) => break,
|
|
|
|
}
|
|
|
|
i = i + 1;
|
|
|
|
}
|
2015-01-05 23:59:45 -06:00
|
|
|
});
|
2014-04-27 22:27:20 -05:00
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let ints = integers();
|
|
|
|
let threes = periodical(3);
|
|
|
|
let fives = periodical(5);
|
2015-01-25 15:05:03 -06:00
|
|
|
for _ in 1..100 {
|
2014-12-23 13:53:35 -06:00
|
|
|
match (ints.recv().unwrap(), threes.recv().unwrap(), fives.recv().unwrap()) {
|
2014-04-27 22:27:20 -05:00
|
|
|
(_, true, true) => println!("FizzBuzz"),
|
|
|
|
(_, true, false) => println!("Fizz"),
|
|
|
|
(_, false, true) => println!("Buzz"),
|
|
|
|
(i, false, false) => println!("{}", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|