Skip to content

Commit c5370be

Browse files
committed
Auto merge of #23816 - frewsxcv:fromiterator-example, r=Manishearth
2 parents b27ba52 + 8fe7f1f commit c5370be

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/libcore/iter.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,27 @@ impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
10171017
built from an iterator over elements of type `{A}`"]
10181018
pub trait FromIterator<A> {
10191019
/// Build a container with elements from something iterable.
1020+
///
1021+
/// # Examples
1022+
///
1023+
/// ```
1024+
/// use std::collections::HashSet;
1025+
/// use std::iter::FromIterator;
1026+
///
1027+
/// let colors_vec = vec!["red", "red", "yellow", "blue"];
1028+
/// let colors_set = HashSet::<&str>::from_iter(colors_vec);
1029+
/// assert_eq!(colors_set.len(), 3);
1030+
/// ```
1031+
///
1032+
/// `FromIterator` is more commonly used implicitly via the `Iterator::collect` method:
1033+
///
1034+
/// ```
1035+
/// use std::collections::HashSet;
1036+
///
1037+
/// let colors_vec = vec!["red", "red", "yellow", "blue"];
1038+
/// let colors_set = colors_vec.into_iter().collect::<HashSet<&str>>();
1039+
/// assert_eq!(colors_set.len(), 3);
1040+
/// ```
10201041
#[stable(feature = "rust1", since = "1.0.0")]
10211042
fn from_iter<T: IntoIterator<Item=A>>(iterator: T) -> Self;
10221043
}

0 commit comments

Comments
 (0)