Rollup merge of #92955 - llogiq:cloned-side-effect-doc, r=yaahc
add perf side effect docs to `Iterator::cloned()` Now that #90209 has been closed, as the current state of affairs is neither here nor there, this at least adds a paragraph + example on what to expect performance-wise and how to deal with it to the .cloned() docs. cc `@the8472`
This commit is contained in:
commit
0e86cabdce
@ -3189,6 +3189,10 @@ fn copied<'a, T: 'a>(self) -> Copied<Self>
|
||||
/// 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
|
||||
@ -3206,6 +3210,18 @@ fn copied<'a, T: 'a>(self) -> Copied<Self>
|
||||
/// 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
|
||||
|
Loading…
Reference in New Issue
Block a user