mirror of
https://github.com/rust-lang/rust
synced 2024-11-02 10:21:05 +00:00
Expand docs for recv
Add an example, plus some text that covers the buffering nature of channels. Fixes #26497
This commit is contained in:
parent
a9e26b5ced
commit
55641720aa
1 changed files with 42 additions and 0 deletions
|
@ -768,6 +768,48 @@ pub fn try_recv(&self) -> Result<T, TryRecvError> {
|
|||
/// If the corresponding `Sender` has disconnected, or it disconnects while
|
||||
/// this call is blocking, this call will wake up and return `Err` to
|
||||
/// indicate that no more messages can ever be received on this channel.
|
||||
/// However, since channels are buffered, messages sent before the disconnect
|
||||
/// will still be properly received.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::sync::mpsc;
|
||||
/// use std::thread;
|
||||
///
|
||||
/// let (send, recv) = mpsc::channel();
|
||||
/// let handle = thread::spawn(move || {
|
||||
/// send.send(1u8).unwrap();
|
||||
/// });
|
||||
///
|
||||
/// handle.join().unwrap();
|
||||
///
|
||||
/// assert_eq!(Ok(1), recv.recv());
|
||||
/// ```
|
||||
///
|
||||
/// Buffering behavior:
|
||||
///
|
||||
/// ```
|
||||
/// use std::sync::mpsc;
|
||||
/// use std::thread;
|
||||
/// use std::sync::mpsc::RecvError;
|
||||
///
|
||||
/// let (send, recv) = mpsc::channel();
|
||||
/// let handle = thread::spawn(move || {
|
||||
/// send.send(1u8).unwrap();
|
||||
/// send.send(2).unwrap();
|
||||
/// send.send(3).unwrap();
|
||||
/// drop(send);
|
||||
/// });
|
||||
///
|
||||
/// // wait for the thread to join so we ensure the sender is dropped
|
||||
/// handle.join().unwrap();
|
||||
///
|
||||
/// assert_eq!(Ok(1), recv.recv());
|
||||
/// assert_eq!(Ok(2), recv.recv());
|
||||
/// assert_eq!(Ok(3), recv.recv());
|
||||
/// assert_eq!(Err(RecvError), recv.recv());
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn recv(&self) -> Result<T, RecvError> {
|
||||
loop {
|
||||
|
|
Loading…
Reference in a new issue