]> git.lizzy.rs Git - rust.git/commitdiff
Add an example for FromIterator::from_iter
authorCorey Farwell <coreyf@rwell.org>
Sat, 28 Mar 2015 21:28:58 +0000 (17:28 -0400)
committerCorey Farwell <coreyf@rwell.org>
Sun, 29 Mar 2015 14:32:53 +0000 (10:32 -0400)
src/libcore/iter.rs

index bb057c553db05af4726e6a062c4e0a4caeca30e1..39bf97ae1ce1c063c302234c17f82d872257d1b7 100644 (file)
@@ -1017,6 +1017,27 @@ fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
                           built from an iterator over elements of type `{A}`"]
 pub trait FromIterator<A> {
     /// Build a container with elements from something iterable.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::HashSet;
+    /// use std::iter::FromIterator;
+    ///
+    /// let colors_vec = vec!["red", "red", "yellow", "blue"];
+    /// let colors_set = HashSet::<&str>::from_iter(colors_vec);
+    /// assert_eq!(colors_set.len(), 3);
+    /// ```
+    ///
+    /// `FromIterator` is more commonly used implicitly via the `Iterator::collect` method:
+    ///
+    /// ```
+    /// use std::collections::HashSet;
+    ///
+    /// let colors_vec = vec!["red", "red", "yellow", "blue"];
+    /// let colors_set = colors_vec.into_iter().collect::<HashSet<&str>>();
+    /// assert_eq!(colors_set.len(), 3);
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     fn from_iter<T: IntoIterator<Item=A>>(iterator: T) -> Self;
 }