Skip to content

Instantly share code, notes, and snippets.

@secunit64
Forked from quux00/chinese-whispers.rs
Created January 20, 2014 08:20
Show Gist options
  • Select an option

  • Save secunit64/8516689 to your computer and use it in GitHub Desktop.

Select an option

Save secunit64/8516689 to your computer and use it in GitHub Desktop.
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