diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 6166be23..e1d90801 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -129,6 +129,7 @@ - [Default Methods](traits/default-methods.md) - [Important Traits](traits/important-traits.md) - [`Iterator`](traits/iterator.md) + - [`FromIterator`](traits/from-iterator.md) - [`From` and `Into`](traits/from-into.md) - [`Read` and `Write`](traits/read-write.md) - [`Add`, `Mul`, ...](traits/operators.md) diff --git a/src/traits/from-iterator.md b/src/traits/from-iterator.md new file mode 100644 index 00000000..4eb89f15 --- /dev/null +++ b/src/traits/from-iterator.md @@ -0,0 +1,23 @@ +# FromIterator + +`FromIterator` lets you build a collection from an `Iterator`. + +```rust,editable +fn main() { + let primes = vec![2, 3, 5, 7]; + let prime_squares = primes.into_iter().map(|prime| prime * prime).collect::>(); +} +``` + +
+ +`Iterator` implements +`fn collect(self) -> B +where + B: FromIterator, + Self: Sized` + +There are also implementations which let you do cool things like convert an +`Iterator>` into a `Result, E>`. + +
diff --git a/src/traits/iterator.md b/src/traits/iterator.md index ae0cc90b..37963075 100644 --- a/src/traits/iterator.md +++ b/src/traits/iterator.md @@ -26,3 +26,10 @@ fn main() { } } ``` + +
+ +`IntoIterator` is the trait that makes for loops work. It is implemented by collection types such as +`Vec` and references to them such as `&Vec` and `&[T]`. Ranges also implement it. + +