]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/iter/traits/iterator.rs
Auto merge of #84115 - CDirkx:rt, r=m-ou-se
[rust.git] / library / core / src / iter / traits / iterator.rs
index 057004f1912033c9997d7666d9e8441c1c99a537..7977d599ae725a0b88c0d8f478fe6bc5218da415 100644 (file)
@@ -944,7 +944,7 @@ fn enumerate(self) -> Enumerate<Self>
     /// Note that the underlying iterator is still advanced when [`peek`] or
     /// [`peek_mut`] are called for the first time: In order to retrieve the
     /// next element, [`next`] is called on the underlying iterator, hence any
-    /// side effects (i.e.  anything other than fetching the next value) of
+    /// side effects (i.e. anything other than fetching the next value) of
     /// the [`next`] method will occur.
     ///
     ///
@@ -982,7 +982,7 @@ fn enumerate(self) -> Enumerate<Self>
     ///
     /// let mut iter = xs.iter().peekable();
     ///
-    /// // peek_mut() lets us see into the future
+    /// // `peek_mut()` lets us see into the future
     /// assert_eq!(iter.peek_mut(), Some(&mut &1));
     /// assert_eq!(iter.peek_mut(), Some(&mut &1));
     /// assert_eq!(iter.next(), Some(&1));
@@ -1495,7 +1495,12 @@ fn flatten(self) -> Flatten<Self>
     /// [`Some(T)`] again. `fuse()` adapts an iterator, ensuring that after a
     /// [`None`] is given, it will always return [`None`] forever.
     ///
+    /// Note that the [`Fuse`] wrapper is a no-op on iterators that implement
+    /// the [`FusedIterator`] trait. `fuse()` may therefore behave incorrectly
+    /// if the [`FusedIterator`] trait is improperly implemented.
+    ///
     /// [`Some(T)`]: Some
+    /// [`FusedIterator`]: crate::iter::FusedIterator
     ///
     /// # Examples
     ///
@@ -1646,31 +1651,16 @@ fn inspect<F>(self, f: F) -> Inspect<Self, F>
     /// Basic usage:
     ///
     /// ```
-    /// let a = [1, 2, 3];
-    ///
-    /// let iter = a.iter();
+    /// let mut words = vec!["hello", "world", "of", "Rust"].into_iter();
     ///
-    /// let sum: i32 = iter.take(5).fold(0, |acc, i| acc + i);
+    /// // Take the first two words.
+    /// let hello_world: Vec<_> = words.by_ref().take(2).collect();
+    /// assert_eq!(hello_world, vec!["hello", "world"]);
     ///
-    /// assert_eq!(sum, 6);
-    ///
-    /// // if we try to use iter again, it won't work. The following line
-    /// // gives "error: use of moved value: `iter`
-    /// // assert_eq!(iter.next(), None);
-    ///
-    /// // let's try that again
-    /// let a = [1, 2, 3];
-    ///
-    /// let mut iter = a.iter();
-    ///
-    /// // instead, we add in a .by_ref()
-    /// let sum: i32 = iter.by_ref().take(2).fold(0, |acc, i| acc + i);
-    ///
-    /// assert_eq!(sum, 3);
-    ///
-    /// // now this is just fine:
-    /// assert_eq!(iter.next(), Some(&3));
-    /// assert_eq!(iter.next(), None);
+    /// // Collect the rest of the words.
+    /// // We can only do this because we used `by_ref` earlier.
+    /// let of_rust: Vec<_> = words.collect();
+    /// assert_eq!(of_rust, vec!["of", "Rust"]);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     fn by_ref(&mut self) -> &mut Self