about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTshepang Lekhonkhobe <tshepang@gmail.com>2015-05-24 23:38:26 +0200
committerTshepang Lekhonkhobe <tshepang@gmail.com>2015-05-25 00:22:44 +0200
commit2d5d6fbca49c29287c29aa8194be79db6b7de89f (patch)
tree1d061b2c76848bcfb40739711026f377d2cfe9c5 /src
parentba0e1cd8147d452c356aacb29fb87568ca26f111 (diff)
downloadrust-2d5d6fbca49c29287c29aa8194be79db6b7de89f.tar.gz
rust-2d5d6fbca49c29287c29aa8194be79db6b7de89f.zip
doc: add example for Iterator::cloned()
Diffstat (limited to 'src')
-rw-r--r--src/libcore/iter.rs15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index ed7cdbbb6e2..5042d44b288 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -1009,8 +1009,19 @@ pub trait Iterator {
         (ts, us)
     }
 
-    /// Creates an iterator that clones the elements it yields. Useful for
-    /// converting an Iterator<&T> to an Iterator<T>.
+    /// Creates an iterator that clones the elements it yields.
+    ///
+    /// This is useful for converting an Iterator<&T> to an Iterator<T>,
+    /// so it's a more convenient form of `map(|&x| x)`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let a = [0, 1, 2];
+    /// let v_cloned: Vec<_> = a.iter().cloned().collect();
+    /// let v_map: Vec<_> = a.iter().map(|&x| x).collect();
+    /// assert_eq!(v_cloned, v_map);
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     fn cloned<'a, T: 'a>(self) -> Cloned<Self>
         where Self: Sized + Iterator<Item=&'a T>, T: Clone