From b1f59b7e58d5a346191c19865dee0a188eff2465 Mon Sep 17 00:00:00 2001 From: Rod Elias Date: Thu, 15 Oct 2020 22:49:10 -0300 Subject: [PATCH] Update README.md Add some fixes. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ba3dce2..9db1c8a 100644 --- a/README.md +++ b/README.md @@ -5750,7 +5750,7 @@ impl Iterator for Alternate { fn main() {} ``` -You can see that under `impl Iterator for Alternate` it says `type Item = i32`. This is the associated type. Our iterator will be for our list of books, which is a `Vec>`. When we call next, it will give us a `String`. So we will write `type Item = String;`. That is the associated item. +You can see that under `impl Iterator for Alternate` it says `type Item = i32`. This is the associated type. Our iterator will be for our list of books, which is a `Vec`. When we call next, it will give us a `String`. So we will write `type Item = String;`. That is the associated item. To implement `Iterator`, you need to write the `fn next()` function. This is where you decide what the iterator should do. For our `Library`, we want it to give us the last books first. So we will `match` with `.pop()` which takes the last item off if it is `Some`. We also want to print " is found!" for each item. Now it looks like this: @@ -5993,8 +5993,8 @@ This is a **warning**, so it's not an error: the program runs fine. But why does - `let num_vec = vec![10, 9, 8];` Right now it is a `Vec`. - `.iter()` Now it is an `Iter`. So it is an iterator with items of `i32`. -- `.enumerate()` Now it is an `Enumerate>`. So it is a type `Enumerate` of type `Item` of `i32`s. -- `.map()` Now it is a type `Map>>`. So it is a type `Map` of type `Enumerate` of type `Item` of `i32`s. +- `.enumerate()` Now it is an `Enumerate>`. So it is a type `Enumerate` of type `Iter` of `i32`s. +- `.map()` Now it is a type `Map>>`. So it is a type `Map` of type `Enumerate` of type `Iter` of `i32`s. All we did was make a more and more complicated structure. So this `Map>>` is a structure that is ready to go, but only when we tell it what to do. Rust does this because it needs to be fast. It doesn't want to do this: @@ -6127,7 +6127,7 @@ This prints `["June", "July"]`. `.filter_map()`. This is called `filter_map()` because it does `.filter()` and `.map()`. The closure must return an `Option`, and then `filter_map()` takes the value out of each `Option` if it is `Some`. So for example if you were to `.filter_map()` a `vec![Some(2), None, Some(3)]`, it would return `[2, 3]`. -We will write an example with a `Company` struct. Each company has a `name` so that field is `String`, but the CEO might have recently quit. So the `ceo` field is `Some`. We will `.filter_map()` over some companies to just keep the CEO names. +We will write an example with a `Company` struct. Each company has a `name` so that field is `String`, but the CEO might have recently quit. So the `ceo` field is `Option`. We will `.filter_map()` over some companies to just keep the CEO names. ```rust struct Company {