about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndre Bogus <bogusandre@gmail.com>2022-01-16 01:33:19 +0100
committerAndre Bogus <bogusandre@gmail.com>2022-03-22 19:07:23 +0100
commit1fb43f66624554d3fd63afc8e141386cbd6d414b (patch)
treec454cca5681cc7d3b5cd189755ea6cf04cf39530
parent3ea44938e21f0de8ae7d4f6399a8a30f97867c70 (diff)
downloadrust-1fb43f66624554d3fd63afc8e141386cbd6d414b.tar.gz
rust-1fb43f66624554d3fd63afc8e141386cbd6d414b.zip
add perf side effect docs to `Iterator::cloned()`
-rw-r--r--library/core/src/iter/traits/iterator.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs
index b62e8dfe1d6..53fbe4cbc42 100644
--- a/library/core/src/iter/traits/iterator.rs
+++ b/library/core/src/iter/traits/iterator.rs
@@ -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
@@ -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