2014-04-27 22:27:20 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-03-05 20:33:58 -06:00
|
|
|
#![feature(std_misc)]
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|