How it works...
Channels are multi-producer-single-consumer data structures, consisting of many senders (with a lightweight clone) but only a single receiver. Under the hood, the channel does not lock but relies on an unsafe data structure that allows the detection and management of the state of the stream. The channel handles simply sending data across threads well and can be used to create an actor-style framework or a reactive map-reduce style data-processing engine.
This is one example of how Rust does fearless concurrency: the data going in is owned by the channel until the receiver retrieves it, which is when a new owner takes over. The channel also acts as a queue and holds elements until they are retrieved. This not only frees the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access