Skip to content

Commit

Permalink
add perf side effect docs to Iterator::cloned()
Browse files Browse the repository at this point in the history
  • Loading branch information
llogiq committed Mar 22, 2022
1 parent 3ea4493 commit 1fb43f6
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,10 @@ pub trait Iterator {
/// This is useful when you have an iterator over `&T`, but you need an
/// iterator over `T`.
///
/// There is no guarantee whatsoever about the `clone` method actually
/// being called *or* optimized away. So code should not depend on
/// either.
///
/// [`clone`]: Clone::clone
///
/// # Examples
Expand All @@ -3206,6 +3210,18 @@ pub trait Iterator {
/// assert_eq!(v_cloned, vec![1, 2, 3]);
/// assert_eq!(v_map, vec![1, 2, 3]);
/// ```
///
/// To get the best performance, try to clone late:
///
/// ```
/// let a = [vec![0_u8, 1, 2], vec![3, 4], vec![23]];
/// // don't do this:
/// let slower: Vec<_> = a.iter().cloned().filter(|s| s.len() == 1).collect();
/// assert_eq!(&[vec![23]], &slower[..]);
/// // instead call `cloned` late
/// let faster: Vec<_> = a.iter().filter(|s| s.len() == 1).cloned().collect();
/// assert_eq!(&[vec![23]], &faster[..]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn cloned<'a, T: 'a>(self) -> Cloned<Self>
where
Expand Down

0 comments on commit 1fb43f6

Please sign in to comment.