1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-10 00:44:21 +02:00

Add speaker notes for Iterator, and an example using FromIterator.

This commit is contained in:
Andrew Walbran 2023-01-16 12:31:24 +00:00
parent 61732adadd
commit 832c7e9963
3 changed files with 31 additions and 0 deletions

View File

@ -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)

View File

@ -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::<Vec<_>>();
}
```
<details>
`Iterator` implements
`fn collect<B>(self) -> B
where
B: FromIterator<Self::Item>,
Self: Sized`
There are also implementations which let you do cool things like convert an
`Iterator<Item = Result<V, E>>` into a `Result<Vec<V>, E>`.
</details>

View File

@ -26,3 +26,10 @@ fn main() {
}
}
```
<details>
`IntoIterator` is the trait that makes for loops work. It is implemented by collection types such as
`Vec<T>` and references to them such as `&Vec<T>` and `&[T]`. Ranges also implement it.
</details>