Skip to content

Commit

Permalink
Clarify iterator by_ref docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hkBst committed Feb 11, 2025
1 parent 061ee95 commit daa5339
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1825,10 +1825,20 @@ pub trait Iterator {
Inspect::new(self, f)
}

/// Borrows an iterator, rather than consuming it.
/// Iterator adapter that turns ownership-taking methods into mutating methods.
///
/// This is useful to allow applying iterator adapters while still
/// retaining ownership of the original iterator.
/// Most `Iterator` methods, other than the `next` method (and this one),
/// take ownership (via a `self` parameter) of the iterator and then mutate it.
/// This method retuns a mutable reference to the original iterator,
/// that acts like the original iterator in all respects,
/// except that ownership-taking methods take ownership of the reference instead of of the original.
/// All mutation by ownership-taking methods will still mutate the original iterator.
///
/// # Technical details
///
/// The mutable reference that this method returns, implements the `Iterator` trait via:
/// [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).
/// This implementation simply passes all method calls on to the original iterator.
///
/// # Examples
///
Expand Down Expand Up @@ -4019,6 +4029,7 @@ where
}
}

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

0 comments on commit daa5339

Please sign in to comment.