Skip to content

Instantly share code, notes, and snippets.

@quux00
Created January 16, 2014 04:58
Show Gist options
  • Save quux00/8450017 to your computer and use it in GitHub Desktop.
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
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