liveBook · Manning

Client

As explained in Learn Rust in a Month of Lunches

Receiver is a Rust standard-library channel handle that obtains values transmitted by corresponding senders.

A client, represented by Rust’s Receiver, is the channel handle that obtains values sent by one or more matching Sender handles. A channel connects both handles to the same value type. section 12.5.1 section 12.5

Use the Receiver to:

  • Call recv() to wait for the next value; it returns a Result and blocks while a Sender remains alive without sending. section 12.5.2
  • Call try_recv() to check immediately instead of waiting; it can return before a value arrives. section 12.5.2
  • Continue receiving queued values after a Sender is dropped, then receive an error when no senders or pending values remain. section 12.5.2
  • Receive messages from several threads when cloned Senders send into the same channel. section 12.5.2

Receive one value

Create the pair, send an i32, and print what the client receives: let (sender, receiver) = channel(); sender.send(5).unwrap(); println!("{}", receiver.recv().unwrap()); The value 5 travels from the Sender to the Receiver. section 12.5.2

use std::sync::mpsc::channel;
 
fn main() {
    let (sender, receiver) = channel();
 
    sender.send(5).unwrap();
    println!("{}", receiver.recv().unwrap());
}

Inspect the complete minimal channel example, including result handling and output from recv(). section 12.5.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains channel pairing, blocking and nonblocking methods, dropped handles, queued values, and Result-based failures.
Examples
Demonstrates receiving 5, consuming messages from cloned senders, and observing RecvError after queued values are exhausted.
What you can do after
Apply via std::sync::mpsc::channel by receiving values, choosing blocking or immediate checks, and handling closed channels.
How it connects
Connects Receiver with channels, Sender, threads, generics, and Result-based error handling.

Also explained in

Compare how 15 books explain client →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.