about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2025-03-11 13:30:49 +0100
committerGitHub <noreply@github.com>2025-03-11 13:30:49 +0100
commitbb2324a6569c6c02991612973e51a996e8b0c966 (patch)
tree0711f0ae9dc173ab52e8fcf3088b29f350e97861
parent705421b52239d7393b4738764b192179d5c139c2 (diff)
parent506c304654156257498f9e1b3ff98600d8a3acf2 (diff)
downloadrust-bb2324a6569c6c02991612973e51a996e8b0c966.tar.gz
rust-bb2324a6569c6c02991612973e51a996e8b0c966.zip
Rollup merge of #135987 - hkBst:patch-20, r=joboet
Clarify iterator by_ref docs

fixes #95143
-rw-r--r--library/core/src/iter/traits/iterator.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs
index 42886e90f99..10ae43ac3fc 100644
--- a/library/core/src/iter/traits/iterator.rs
+++ b/library/core/src/iter/traits/iterator.rs
@@ -1825,10 +1825,19 @@ pub trait Iterator {
         Inspect::new(self, f)
     }
 
-    /// Borrows an iterator, rather than consuming it.
+    /// Creates a "by reference" adapter for this instance of `Iterator`.
     ///
-    /// This is useful to allow applying iterator adapters while still
-    /// retaining ownership of the original iterator.
+    /// Consuming method calls (direct or indirect calls to `next`)
+    /// on the "by reference" adapter will consume the original iterator,
+    /// but ownership-taking methods (those with a `self` parameter)
+    /// only take ownership of the "by reference" iterator.
+    ///
+    /// This is useful for applying ownership-taking methods
+    /// (such as `take` in the example below)
+    /// without giving up ownership of the original iterator,
+    /// so you can use the original iterator afterwards.
+    ///
+    /// Uses [impl<I: Iterator + ?Sized> Iterator for &mut I { type Item = I::Item; ...}](https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#impl-Iterator-for-%26mut+I).
     ///
     /// # Examples
     ///
@@ -4024,6 +4033,9 @@ where
     }
 }
 
+/// Implements `Iterator` for mutable references to iterators, such as those produced by [`Iterator::by_ref`].
+///
+/// This implementation passes all method calls on to the original iterator.
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<I: Iterator + ?Sized> Iterator for &mut I {
     type Item = I::Item;