Skip to content

Commit b602dc9

Browse files
authored
Merge pull request #169 from google/intoiterator
Add some speaker notes for day 3 morning and a page about FromIterator
2 parents 61732ad + 1c7ce1c commit b602dc9

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
- [Default Methods](traits/default-methods.md)
130130
- [Important Traits](traits/important-traits.md)
131131
- [`Iterator`](traits/iterator.md)
132+
- [`FromIterator`](traits/from-iterator.md)
132133
- [`From` and `Into`](traits/from-into.md)
133134
- [`Read` and `Write`](traits/read-write.md)
134135
- [`Add`, `Mul`, ...](traits/operators.md)

src/generics/closures.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ fn main() {
1919
println!("mul_5: {}", apply_with_log(mul_5, 20));
2020
}
2121
```
22+
23+
<details>
24+
25+
If you have an `FnOnce`, you may only call it once. It might consume captured values.
26+
27+
An `FnMut` might mutate captured values, so you can call it multiple times but not concurrently.
28+
29+
An `Fn` neither consumes nor mutates captured values, or perhaps captures nothing at all, so it can
30+
be called multiple times concurrently.
31+
32+
`FnMut` is a subtype of `FnOnce`. `Fn` is a subtype of `FnMut` and `FnOnce`. I.e. you can use an
33+
`FnMut` wherever an `FnOnce` is called for, and you can use an `Fn` wherever an `FnMut` or `FnOnce`
34+
is called for.
35+
36+
`move` closures only implement `FnOnce`.
37+
38+
</details>

src/generics/impl-trait.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ fn main() {
1818

1919
* `impl Trait` cannot be used with the `::<>` turbo fish syntax.
2020
* `impl Trait` allows you to work with types which you cannot name.
21+
22+
<details>
23+
24+
The meaning of `impl Trait` is a bit different in the different positions.
25+
26+
* For a parameter, `impl Trait` is like an anonymous generic parameter with a trait bound.
27+
* For a return type, it means that the return type is some concrete type that implements the trait,
28+
without naming the type. This can be useful when you don't want to expose the concrete type in a
29+
public API.
30+
31+
</details>

src/generics/trait-objects.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ Memory layout after allocating `xs`:
5454
: | | '---->| "<str as Display>::fmt" | :
5555
: | | +-------------------------+ :
5656
: | | :
57-
: | | +-------------------------+ :
58-
: | '-->| "<i32 as Display>::fmt" | :
59-
: | +-------------------------+ :
60-
: | :
57+
: | | +----+----+----+----+ :
58+
: | '-->| 7b | 00 | 00 | 00 | :
6159
: | +----+----+----+----+ :
62-
: '---->| 7b | 00 | 00 | 00 | :
63-
: +----+----+----+----+ :
60+
: | :
61+
: | +-------------------------+ :
62+
: '---->| "<i32 as Display>::fmt" | :
63+
: +-------------------------+ :
6464
: :
6565
: :
6666
'- - - - - - - - - - - - - - - - - - - - - - - -'

src/traits/from-iterator.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# FromIterator
2+
3+
`FromIterator` lets you build a collection from an `Iterator`.
4+
5+
```rust,editable
6+
fn main() {
7+
let primes = vec![2, 3, 5, 7];
8+
let prime_squares = primes.into_iter().map(|prime| prime * prime).collect::<Vec<_>>();
9+
}
10+
```
11+
12+
<details>
13+
14+
`Iterator` implements
15+
`fn collect<B>(self) -> B
16+
where
17+
B: FromIterator<Self::Item>,
18+
Self: Sized`
19+
20+
There are also implementations which let you do cool things like convert an
21+
`Iterator<Item = Result<V, E>>` into a `Result<Vec<V>, E>`.
22+
23+
</details>

src/traits/iterator.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ fn main() {
2626
}
2727
}
2828
```
29+
30+
<details>
31+
32+
`IntoIterator` is the trait that makes for loops work. It is implemented by collection types such as
33+
`Vec<T>` and references to them such as `&Vec<T>` and `&[T]`. Ranges also implement it.
34+
35+
</details>

0 commit comments

Comments
 (0)