Created
April 1, 2013 00:26
-
-
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
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
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()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to remove all the capture clauses. i.e.
do spawn { ... }
instead ofdo spawn |foo, bar| { ... }