Skip to content

Instantly share code, notes, and snippets.

@edwardw
Last active December 15, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardw/5177250 to your computer and use it in GitHub Desktop.
Save edwardw/5177250 to your computer and use it in GitHub Desktop.
Go Concurrency Challenge in Rust

An adaption of Leah Hanson's solution using Rust macros. The original go challenge goes here.

Compiled with Rust 0.5.

use core::task::spawn;
use core::pipes::{stream,Port,Chan,SharedChan};

macro_rules! compute_and_send(
    ($inp:expr, $port:ident) => (
        do spawn {
            $port.send($inp);
        }
    );
)

fn main() {
    let (out,in): (Port<int>, Chan<int>) = stream();
    let in = SharedChan(in);
    let (strout,strin): (Port<~str>, Chan<~str>) = stream();

    do spawn {
        let x = out.recv();
        let y = out.recv();
        let z = out.recv();
        let result = fmt!("%d + %d + %d = %d", x, y, z, x+y+z);
        strin.send(result);
    }

    let (in1, in2, in3) = (in.clone(), in.clone(), in.clone());
    compute_and_send!(2 * 10, in1);
    compute_and_send!(2 * 20, in2);
    compute_and_send!(30 + 40, in3);

    let result = strout.recv();
    io::println(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment