3 ms·
This is completely the opposite: in Rust you have an explicit sender and an explicit receiver, and you have no control on the channel that was created. Only thi
by rakoo 12y ago
This is completely the opposite: in Rust you have an explicit sender and an explicit receiver, and you have no control on the channel that was created. Only this sender can send to the pipe, and only this receiver can receive.
In go, all you have is the channel. Anyone can send to it/read from it:
c := make(chan int)
go func() {
// I can write here
c <- 1
}()
go func() {
// I can also write here
c <- 2
}()
// I can read here, even though I have no idea who wrote to the channel
for a := <- c {
// Note that since I don't know who wrote to c, I can't expect
// any order here. All I can do is process stuff that is coming
// down the pipe (which is all I really care about after all
}
- dragonwriter 12y ago> This is completely the opposite: in Rust you have an explicit sender and an explicit receiver, and you have no control on the channel that was created. Only this sender can send to the pipe, and only this receiver can receive. Its not "completely the opposite", though it is different. Rust doesn't let multiple tasks use the same sender handle, but supports multiple senders on the same channel by cloning the sender handle (same with the receiver handle, mutatis mutandis.) So your Go code becomes something like this in Rust (the only substantive difference is the need to clone the sender handle): let (tx, rx) = channel(); spawn(proc() { tx.send(1); }); tx2 = tx.clone(); spawn(proc() { tx2.send(2); }); for a in rx.iter() { // As in the Go example, process the stuff coming down // the channel with no expected order or knowledge of // who sent to it. }