rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.44k stars 12.6k forks source link

SharedChan in Tasks and Communication Tutorial #9573

Closed simao closed 10 years ago

simao commented 11 years ago

Hello,

I am reading the Rust Tasks and Communication Tutorial and I am trying to run the following code:

use std::uint;
use std::comm;

fn main() {
    let (port, chan) = stream();
    let chan = SharedChan::new(chan);

    for uint::range(0, 3) |init_val| {
        // Create a new channel handle to distribute to the child task
        let child_chan = chan.clone();

        do spawn {
            child_chan.send(init_val);
        }
    }

    let result = port.recv() + port.recv() + port.recv();
}

But I get the following error:

test.rs:8:15: 8:30 error: unresolved name
test.rs:8     let chan = SharedChan::new(chan);
                         ^~~~~~~~~~~~~~~

I thought SharedChan was defined in std::comm ?

Thanks

metajack commented 11 years ago

It is, but you have to use std::com::SharedChan; or use it like let chan = comm::SharedChan::new(chan);.

Tutorial will need fixing of course.

simao commented 11 years ago

Ah thanks, that works!

Just checked the markdown source for the tutorial and it includes:

# use std::task::spawn;
# use std::comm::{stream, SharedChan};

But that's not rendered on the html version.

On Fri, Sep 27, 2013 at 8:33 PM, Jack Moffitt notifications@github.comwrote:

It is, but you have to use std::com::SharedChan; or use it like let chan = comm::SharedChan::new(chan);.

Tutorial will need fixing of course.

— Reply to this email directly or view it on GitHubhttps://github.com/mozilla/rust/issues/9573#issuecomment-25271015 .

wting commented 10 years ago

This can be closed, the following code is from the tutorial and works as expected (without explicit imports):

fn main() {
    // single sender and receiver
    let (port, chan): (Port<int>, Chan<int>) = Chan::new();
    do spawn {
        chan.send(5);
    }

    println!("{:d}", port.recv());

    // multiple senders
    let (port, chan) = SharedChan::new();

    for i in range(0, 3) {
        let child_chan = chan.clone();
        do spawn {
            child_chan.send(i);
        }
    }

    println!("{:d} {:d} {:d}", port.recv(), port.recv(), port.recv());
}