From eb48ee72dba6646d92fb06e384d396759ff80f59 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 2 Jun 2017 00:20:25 -0400 Subject: [PATCH] Rewrite `Receiver::try_iter` doc example to show resulting values. --- src/libstd/sync/mpsc/mod.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index 8fdd99e86b087..7d5e1929cd2a1 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -1395,29 +1395,34 @@ impl Receiver { /// /// # Examples /// - /// ```rust + /// ```no_run /// use std::sync::mpsc::channel; /// use std::thread; /// use std::time::Duration; /// /// let (sender, receiver) = channel(); /// - /// // Nothing is in the buffer yet + /// // nothing is in the buffer yet /// assert!(receiver.try_iter().next().is_none()); - /// println!("Nothing in the buffer..."); /// /// thread::spawn(move || { + /// thread::sleep(Duration::from_secs(1)); /// sender.send(1).unwrap(); /// sender.send(2).unwrap(); /// sender.send(3).unwrap(); /// }); /// - /// println!("Going to sleep..."); - /// thread::sleep(Duration::from_secs(2)); // block for two seconds + /// // nothing is in the buffer yet + /// assert!(receiver.try_iter().next().is_none()); /// - /// for x in receiver.try_iter() { - /// println!("Got: {}", x); - /// } + /// // block for two seconds + /// thread::sleep(Duration::from_secs(2)); + /// + /// let mut iter = receiver.try_iter(); + /// assert_eq!(iter.next(), Some(1)); + /// assert_eq!(iter.next(), Some(2)); + /// assert_eq!(iter.next(), Some(3)); + /// assert_eq!(iter.next(), None); /// ``` #[stable(feature = "receiver_try_iter", since = "1.15.0")] pub fn try_iter(&self) -> TryIter {