Created
January 16, 2014 04:58
-
-
Save quux00/8450017 to your computer and use it in GitHub Desktop.
Chinese Whispers in Rust
Based on example by Rob Pike in http://www.youtube.com/watch?v=f6kdp27TYZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static N:int = 100000; | |
// | |
// Chinese Whispers in Rust | |
// Based on example by Rob Pike | |
// in http://www.youtube.com/watch?v=f6kdp27TYZs | |
// | |
fn main() { | |
let (leftmost_port, leftmost_chan) = Chan::new(); | |
let mut leftc: Chan<int> = leftmost_chan; | |
let mut rightp: Port<int>; | |
for _ in range(0, N) { | |
let (right_port, right_chan) = Chan::new(); | |
rightp = right_port; | |
let c = leftc; | |
let p = rightp; | |
do spawn { | |
whisper(c, p); | |
} | |
leftc = right_chan; | |
} | |
let rightmost_chan = leftc; | |
do spawn { | |
rightmost_chan.send(1); | |
} | |
println!("{}", leftmost_port.recv()); | |
} | |
fn whisper(left: Chan<int>, right: Port<int>) { | |
left.send( right.recv() + 1 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment