From 832c7e99635a22d3cf71fc594b7d19d2a7f19748 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 16 Jan 2023 12:31:24 +0000 Subject: [PATCH 1/4] Add speaker notes for Iterator, and an example using FromIterator. --- src/SUMMARY.md | 1 + src/traits/from-iterator.md | 23 +++++++++++++++++++++++ src/traits/iterator.md | 7 +++++++ 3 files changed, 31 insertions(+) create mode 100644 src/traits/from-iterator.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 6166be23..e1d90801 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/traits/from-iterator.md b/src/traits/from-iterator.md new file mode 100644 index 00000000..4eb89f15 --- /dev/null +++ b/src/traits/from-iterator.md @@ -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::>(); +} +``` + +
+ +`Iterator` implements +`fn collect(self) -> B +where + B: FromIterator, + Self: Sized` + +There are also implementations which let you do cool things like convert an +`Iterator>` into a `Result, E>`. + +
diff --git a/src/traits/iterator.md b/src/traits/iterator.md index ae0cc90b..37963075 100644 --- a/src/traits/iterator.md +++ b/src/traits/iterator.md @@ -26,3 +26,10 @@ fn main() { } } ``` + +
+ +`IntoIterator` is the trait that makes for loops work. It is implemented by collection types such as +`Vec` and references to them such as `&Vec` and `&[T]`. Ranges also implement it. + +
From a1861ef90071d05bc583873f4d8a2d8956d13f95 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 16 Jan 2023 14:15:00 +0000 Subject: [PATCH 2/4] Add speaker notes for impl Trait. --- src/generics/impl-trait.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/generics/impl-trait.md b/src/generics/impl-trait.md index 58afd6a5..c7ec317a 100644 --- a/src/generics/impl-trait.md +++ b/src/generics/impl-trait.md @@ -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. + +
+ +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. + +
From 5cbb8d4c27510065d2048784adefa97daa65bc7f Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 16 Jan 2023 15:35:10 +0000 Subject: [PATCH 3/4] Add speaker notes about closures. --- src/generics/closures.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/generics/closures.md b/src/generics/closures.md index 248aa405..83b2000a 100644 --- a/src/generics/closures.md +++ b/src/generics/closures.md @@ -19,3 +19,20 @@ fn main() { println!("mul_5: {}", apply_with_log(mul_5, 20)); } ``` + +
+ +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`. + +
From 1c7ce1cac66a04594413866afd13e545c137b614 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 16 Jan 2023 15:41:17 +0000 Subject: [PATCH 4/4] Use a consistent order in memory layout of trait objects example. --- src/generics/trait-objects.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/generics/trait-objects.md b/src/generics/trait-objects.md index 140fab6f..a5489f55 100644 --- a/src/generics/trait-objects.md +++ b/src/generics/trait-objects.md @@ -54,13 +54,13 @@ Memory layout after allocating `xs`: : | | '---->| "::fmt" | : : | | +-------------------------+ : : | | : - : | | +-------------------------+ : - : | '-->| "::fmt" | : - : | +-------------------------+ : - : | : + : | | +----+----+----+----+ : + : | '-->| 7b | 00 | 00 | 00 | : : | +----+----+----+----+ : - : '---->| 7b | 00 | 00 | 00 | : - : +----+----+----+----+ : + : | : + : | +-------------------------+ : + : '---->| "::fmt" | : + : +-------------------------+ : : : : : '- - - - - - - - - - - - - - - - - - - - - - - -'