Skip to content

Commit 893e416

Browse files
committed
Auto merge of #25758 - tshepang:add-cloned-example, r=Gankro
2 parents 820b1d8 + 2d5d6fb commit 893e416

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/libcore/iter.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,19 @@ pub trait Iterator {
10091009
(ts, us)
10101010
}
10111011

1012-
/// Creates an iterator that clones the elements it yields. Useful for
1013-
/// converting an Iterator<&T> to an Iterator<T>.
1012+
/// Creates an iterator that clones the elements it yields.
1013+
///
1014+
/// This is useful for converting an Iterator<&T> to an Iterator<T>,
1015+
/// so it's a more convenient form of `map(|&x| x)`.
1016+
///
1017+
/// # Examples
1018+
///
1019+
/// ```
1020+
/// let a = [0, 1, 2];
1021+
/// let v_cloned: Vec<_> = a.iter().cloned().collect();
1022+
/// let v_map: Vec<_> = a.iter().map(|&x| x).collect();
1023+
/// assert_eq!(v_cloned, v_map);
1024+
/// ```
10141025
#[stable(feature = "rust1", since = "1.0.0")]
10151026
fn cloned<'a, T: 'a>(self) -> Cloned<Self>
10161027
where Self: Sized + Iterator<Item=&'a T>, T: Clone

0 commit comments

Comments
 (0)