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:
parent
61732adadd
commit
832c7e9963
@ -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)
|
||||
|
23
src/traits/from-iterator.md
Normal file
23
src/traits/from-iterator.md
Normal 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>
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user