O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Channels

Communication between threads can be implemented in a safe way by the use of channels. Rust's standard library has two kinds of channels defined in std::sync::mpsc:

  • channel: This is an asynchronous, infinite buffer
  • sync_channel: This is a synchronous, bounded buffer

The acronym mpsc refers to multi producer, single consumer. That is, these channels may have multiple writers but only a single reader. Both of these functions return a pair of generic values: a sender and a receiver. The sender can be used to push new things into the channel, while receivers can be used to get things from the channel. The sender implements the Clone trait while the receiver does not. This, paired with Rust's regular ownership system, allows the compiler ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required