|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +fn periodical(n: int) -> Receiver<bool> { |
| 12 | + let (chan, port) = channel(); |
| 13 | + spawn(proc() { |
| 14 | + loop { |
| 15 | + for _ in range(1, n) { |
| 16 | + match chan.send_opt(false) { |
| 17 | + Ok(()) => {} |
| 18 | + Err(..) => break, |
| 19 | + } |
| 20 | + } |
| 21 | + match chan.send_opt(true) { |
| 22 | + Ok(()) => {} |
| 23 | + Err(..) => break |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | + return port; |
| 28 | +} |
| 29 | + |
| 30 | +fn integers() -> Receiver<int> { |
| 31 | + let (chan, port) = channel(); |
| 32 | + spawn(proc() { |
| 33 | + let mut i = 1; |
| 34 | + loop { |
| 35 | + match chan.send_opt(i) { |
| 36 | + Ok(()) => {} |
| 37 | + Err(..) => break, |
| 38 | + } |
| 39 | + i = i + 1; |
| 40 | + } |
| 41 | + }); |
| 42 | + return port; |
| 43 | +} |
| 44 | + |
| 45 | +fn main() { |
| 46 | + let ints = integers(); |
| 47 | + let threes = periodical(3); |
| 48 | + let fives = periodical(5); |
| 49 | + for _ in range(1, 100) { |
| 50 | + match (ints.recv(), threes.recv(), fives.recv()) { |
| 51 | + (_, true, true) => println!("FizzBuzz"), |
| 52 | + (_, true, false) => println!("Fizz"), |
| 53 | + (_, false, true) => println!("Buzz"), |
| 54 | + (i, false, false) => println!("{}", i) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
0 commit comments