Bounded task channels
Channel<T> passes typed values between tasks. send waits while a bounded buffer is full, and recv waits while it is empty. Capacity zero hands each value directly from a sender to a receiver.
Closing preserves buffered values, then recv returns none. Sending after close is a runtime error.
async fn produce(channel: Channel<int>) {
await channel.send(1)
await channel.send(2)
channel.close()
}
async fn main() {
let channel = std.channel.new<int>(2)
spawn(produce(channel))
let mut total = 0
while let some(value) = await channel.recv() { total += value }
println(total)
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: 3
Ctrl/⌘+Enter runs the current lesson.