mirror of
https://github.com/Dhghomon/easy_rust
synced 2024-11-05 12:00:23 +00:00
Fix small typo in filter_map() example, get rid of into_iter() for range.
Range already implements Iterator trait, so no need for the into_iter().
This commit is contained in:
parent
36c3e5d0f9
commit
acd5a7ca1b
@ -4895,7 +4895,7 @@ This prints `["June", "July"]`.
|
||||
|
||||
|
||||
|
||||
`.filter_map()`. This is called `filter_map()` because it does `.filter()` and `.map()`. The closure must return an `Option<T>`, 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(9), None, Some(3)]`, it would return `[2, 3]`.
|
||||
`.filter_map()`. This is called `filter_map()` because it does `.filter()` and `.map()`. The closure must return an `Option<T>`, 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<String>`. We will `.filter_map()` over some companies to just keep the CEO names.
|
||||
|
||||
@ -5177,7 +5177,7 @@ Some(5)
|
||||
Some(6)
|
||||
```
|
||||
|
||||
We were right: there is oone `Some(5)` and then the 1000 `Some(6)` start. So we can write this:
|
||||
We were right: there is one `Some(5)` and then the 1000 `Some(6)` start. So we can write this:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
@ -5264,8 +5264,8 @@ Something similar can be done with a range that doesn't have an ending. If you w
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let ten_chars = ('a'..).into_iter().take(10).collect::<Vec<char>>();
|
||||
let skip_then_ten_chars = ('a'..).into_iter().skip(1300).take(10).collect::<Vec<char>>();
|
||||
let ten_chars = ('a'..).take(10).collect::<Vec<char>>();
|
||||
let skip_then_ten_chars = ('a'..).skip(1300).take(10).collect::<Vec<char>>();
|
||||
|
||||
println!("{:?}", ten_chars);
|
||||
println!("{:?}", skip_then_ten_chars);
|
||||
|
Loading…
Reference in New Issue
Block a user