Skip to content

Instantly share code, notes, and snippets.

@graue
Created April 1, 2013 00:26
Show Gist options
  • Select an option

  • Save graue/5282602 to your computer and use it in GitHub Desktop.

Select an option

Save graue/5282602 to your computer and use it in GitHub Desktop.
Attempt to update this program for Rust 0.6: https://gist.github.com/elimisteve/4442820#comment-701921
extern mod std;
use core::task::spawn;
use core::comm::{stream, Port, Chan, SharedChan};
// Purely functional doubler
// No need to combine computation / means of sending it in the same function
// In a real example, this could be an expensive computation
fn double(x: int) -> int {
x * 2
}
fn main() {
// Create port/channel pair for receiving/sending ints, respectively
let (port, chan) : (Port<int>, Chan<int>) = stream();
// To allow multiple tasks to write, turn the channel into a SharedChan
let chan = SharedChan(chan);
// Spawn 3 tasks to process data in the background,
// each writing to its own clone of the SharedChan
let child = chan.clone();
do spawn |child| { child.send(double(10)); }
let child = chan.clone();
do spawn |child| { child.send(double(20)); }
let child = chan.clone();
do spawn |child| { child.send((|a : int, b : int| a+b)(30, 40)); }
// Make port/channel pair for the string result
let (result_port, result_chan) : (Port<~str>, Chan<~str>) = stream();
do spawn |port, result_chan| {
let (x, y, z) = (port.recv(), port.recv(), port.recv());
let result : ~str = fmt!("%d + %d + %d = %d", x, y, z, x+y+z);
result_chan.send(result);
}
// Receive and print result
io::println(result_port.recv());
}
@luqmana

luqmana commented Apr 1, 2013

Copy link
Copy Markdown

You need to remove all the capture clauses. i.e. do spawn { ... } instead of do spawn |foo, bar| { ... }

@graue

graue commented Apr 1, 2013

Copy link
Copy Markdown
Author

Ah, cool. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment