Skip to content

Commit 5022938

Browse files
committed
Clarify iterator by_ref docs
1 parent 061ee95 commit 5022938

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

library/core/src/iter/traits/iterator.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1825,10 +1825,24 @@ pub trait Iterator {
18251825
Inspect::new(self, f)
18261826
}
18271827

1828-
/// Borrows an iterator, rather than consuming it.
1829-
///
1830-
/// This is useful to allow applying iterator adapters while still
1831-
/// retaining ownership of the original iterator.
1828+
/// Most `Iterator` methods, other than the `next` method (and this one),
1829+
/// are consuming methods; they move the iterator, taking ownership of it,
1830+
/// via their `self` parameter (and then mutating it privately).
1831+
/// Usually, this is fine, though sometimes you want to
1832+
/// mutate the original iterator without giving up ownership of it.
1833+
/// That is where this method comes in.
1834+
///
1835+
/// The return value of this method acts like the original iterator,
1836+
/// except that all consuming methods behave like mutating methods,
1837+
/// which mutate the original iterator, without taking ownership of it.
1838+
///
1839+
/// This works, because the mutable reference that this method returns,
1840+
/// implements the `Iterator` trait in its own right via:
1841+
/// [impl<I: Iterator + ?Sized> Iterator for &mut I { type Item = I::Item; ...}](trait.Iterator.html#impl-Iterator-for-%26mut I).
1842+
/// This implementation simply passes all method calls on to the original iterator,
1843+
/// allowing you to call a consuming iterator method on the mutable reference,
1844+
/// which will mutate the original,
1845+
/// without giving up ownership of the original iterator.
18321846
///
18331847
/// # Examples
18341848
///
@@ -4019,6 +4033,7 @@ where
40194033
}
40204034
}
40214035

4036+
/// Implements Iterator for mutable references to Iterator, such as those produced by [Iterator::by_ref]
40224037
#[stable(feature = "rust1", since = "1.0.0")]
40234038
impl<I: Iterator + ?Sized> Iterator for &mut I {
40244039
type Item = I::Item;

0 commit comments

Comments
 (0)