about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2020-11-28 15:58:28 +0100
committerGitHub <noreply@github.com>2020-11-28 15:58:28 +0100
commit248e5ad299de46bcdd8c5d8a35d6d8cd07de54c2 (patch)
tree8ce78ad58b8b9dbac1dd025d6b747d33b98abf76
parentdd0f9533786ddede9307cb57839ca5039669a6e8 (diff)
parent08ec201c725ca86586fbb6b55f0a33272bd45d56 (diff)
downloadrust-248e5ad299de46bcdd8c5d8a35d6d8cd07de54c2.tar.gz
rust-248e5ad299de46bcdd8c5d8a35d6d8cd07de54c2.zip
Rollup merge of #79478 - lukaslueg:peek_mut_docs, r=m-ou-se
Expand docs on Peekable::peek_mut

Slightly expand docs on `std::iter::Peekable::peek_mut`, tracked in #78302

r? `@m-ou-se`
-rw-r--r--library/core/src/iter/adapters/peekable.rs14
1 files changed, 9 insertions, 5 deletions
diff --git a/library/core/src/iter/adapters/peekable.rs b/library/core/src/iter/adapters/peekable.rs
index ebdc2555db2..2f8b9653c59 100644
--- a/library/core/src/iter/adapters/peekable.rs
+++ b/library/core/src/iter/adapters/peekable.rs
@@ -230,20 +230,24 @@ impl<I: Iterator> Peekable<I> {
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
     /// ```
     /// #![feature(peekable_peek_mut)]
     /// let mut iter = [1, 2, 3].iter().peekable();
     ///
+    /// // Like with `peek()`, we can see into the future without advancing the iterator.
+    /// assert_eq!(iter.peek_mut(), Some(&mut &1));
     /// assert_eq!(iter.peek_mut(), Some(&mut &1));
     /// assert_eq!(iter.next(), Some(&1));
     ///
-    /// // Peek into the iterator and modify the value which will be returned next
-    /// if let Some(mut p) = iter.peek_mut() {
-    ///     if *p == &2 {
-    ///         *p = &5;
-    ///     }
+    /// // Peek into the iterator and set the value behind the mutable reference.
+    /// if let Some(p) = iter.peek_mut() {
+    ///     assert_eq!(*p, &2);
+    ///     *p = &5;
     /// }
     ///
+    /// // The value we put in reappears as the iterator continues.
     /// assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
     /// ```
     #[inline]