]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | use std::thread::Thread; | |
12 | use std::sync::mpsc::{channel, Receiver}; | |
13 | ||
14 | fn periodical(n: int) -> Receiver<bool> { | |
15 | let (chan, port) = channel(); | |
16 | Thread::spawn(move|| { | |
17 | loop { | |
85aaf69f | 18 | for _ in 1..n { |
1a4d82fc JJ |
19 | match chan.send(false) { |
20 | Ok(()) => {} | |
21 | Err(..) => break, | |
22 | } | |
23 | } | |
24 | match chan.send(true) { | |
25 | Ok(()) => {} | |
26 | Err(..) => break | |
27 | } | |
28 | } | |
29 | }); | |
30 | return port; | |
31 | } | |
32 | ||
33 | fn integers() -> Receiver<int> { | |
34 | let (chan, port) = channel(); | |
35 | Thread::spawn(move|| { | |
36 | let mut i = 1; | |
37 | loop { | |
38 | match chan.send(i) { | |
39 | Ok(()) => {} | |
40 | Err(..) => break, | |
41 | } | |
42 | i = i + 1; | |
43 | } | |
44 | }); | |
45 | return port; | |
46 | } | |
47 | ||
48 | fn main() { | |
49 | let ints = integers(); | |
50 | let threes = periodical(3); | |
51 | let fives = periodical(5); | |
85aaf69f | 52 | for _ in 1..100 { |
1a4d82fc JJ |
53 | match (ints.recv().unwrap(), threes.recv().unwrap(), fives.recv().unwrap()) { |
54 | (_, true, true) => println!("FizzBuzz"), | |
55 | (_, true, false) => println!("Fizz"), | |
56 | (_, false, true) => println!("Buzz"), | |
57 | (i, false, false) => println!("{}", i) | |
58 | } | |
59 | } | |
60 | } |