mirror of
https://github.com/google/comprehensive-rust.git
synced 2024-12-12 05:24:12 +02:00
Merge pull request #169 from google/intoiterator
Add some speaker notes for day 3 morning and a page about FromIterator
This commit is contained in:
commit
b602dc9be4
@ -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)
|
||||
|
@ -19,3 +19,20 @@ fn main() {
|
||||
println!("mul_5: {}", apply_with_log(mul_5, 20));
|
||||
}
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
If you have an `FnOnce`, you may only call it once. It might consume captured values.
|
||||
|
||||
An `FnMut` might mutate captured values, so you can call it multiple times but not concurrently.
|
||||
|
||||
An `Fn` neither consumes nor mutates captured values, or perhaps captures nothing at all, so it can
|
||||
be called multiple times concurrently.
|
||||
|
||||
`FnMut` is a subtype of `FnOnce`. `Fn` is a subtype of `FnMut` and `FnOnce`. I.e. you can use an
|
||||
`FnMut` wherever an `FnOnce` is called for, and you can use an `Fn` wherever an `FnMut` or `FnOnce`
|
||||
is called for.
|
||||
|
||||
`move` closures only implement `FnOnce`.
|
||||
|
||||
</details>
|
||||
|
@ -18,3 +18,14 @@ fn main() {
|
||||
|
||||
* `impl Trait` cannot be used with the `::<>` turbo fish syntax.
|
||||
* `impl Trait` allows you to work with types which you cannot name.
|
||||
|
||||
<details>
|
||||
|
||||
The meaning of `impl Trait` is a bit different in the different positions.
|
||||
|
||||
* For a parameter, `impl Trait` is like an anonymous generic parameter with a trait bound.
|
||||
* For a return type, it means that the return type is some concrete type that implements the trait,
|
||||
without naming the type. This can be useful when you don't want to expose the concrete type in a
|
||||
public API.
|
||||
|
||||
</details>
|
||||
|
@ -54,13 +54,13 @@ Memory layout after allocating `xs`:
|
||||
: | | '---->| "<str as Display>::fmt" | :
|
||||
: | | +-------------------------+ :
|
||||
: | | :
|
||||
: | | +-------------------------+ :
|
||||
: | '-->| "<i32 as Display>::fmt" | :
|
||||
: | +-------------------------+ :
|
||||
: | :
|
||||
: | | +----+----+----+----+ :
|
||||
: | '-->| 7b | 00 | 00 | 00 | :
|
||||
: | +----+----+----+----+ :
|
||||
: '---->| 7b | 00 | 00 | 00 | :
|
||||
: +----+----+----+----+ :
|
||||
: | :
|
||||
: | +-------------------------+ :
|
||||
: '---->| "<i32 as Display>::fmt" | :
|
||||
: +-------------------------+ :
|
||||
: :
|
||||
: :
|
||||
'- - - - - - - - - - - - - - - - - - - - - - - -'
|
||||
|
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