Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some speaker notes for day 3 morning and a page about FromIterator #169

Merged
merged 4 commits into from Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/generics/closures.md
Expand Up @@ -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>
11 changes: 11 additions & 0 deletions src/generics/impl-trait.md
Expand Up @@ -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>
12 changes: 6 additions & 6 deletions src/generics/trait-objects.md
Expand Up @@ -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" | :
: +-------------------------+ :
: :
: :
'- - - - - - - - - - - - - - - - - - - - - - - -'
Expand Down
23 changes: 23 additions & 0 deletions src/traits/from-iterator.md
@@ -0,0 +1,23 @@
# FromIterator
mgeisler marked this conversation as resolved.
Show resolved Hide resolved

`FromIterator` lets you build a collection from an `Iterator`.
mgeisler marked this conversation as resolved.
Show resolved Hide resolved

```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>
7 changes: 7 additions & 0 deletions src/traits/iterator.md
Expand Up @@ -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>