Skip to content

Commit

Permalink
rephrase compiler advice and filter_map example
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelraz committed Jan 21, 2025
1 parent b246c4c commit 85afd21
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions exercise-book/src/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,30 @@ This `::<SomeType>` syntax is called the [turbo fish operator](https://doc.rust-

### Dealing with `.unwrap()`s in iterator chains

When starting out with iterators, it's very easy to be "led astray" by locally useful `.unwrap()`s as suggested by the compiler.
Intermediate steps in iterator chains often produce `Result` or `Option`.

It's easy to get a slogging first solution with a lot of `Option` and `Result` wrapping and unwrapping that other languages wouldn't make explicit.
You may be compelled to use `unwrap / expect` to get the inner values

Concretely, the following snippet:

```rust
However, there are usually better ways that don't require a potentially panicking method.

let numeric_lines = reader.lines()
.map(|l| l.unwrap())
.map(|s| s.parse::<i32>())
.filter(|s| s.is_ok())
//...
Concretely, the following snippet:

```rust [], ignore
let numbers: Vec<_> = ["1", "2", "3"]
.iter()
.map(|s| s.parse::<i32>())
.filter(|r| r.is_ok())
.map(|r| r.expect("all `Result`s are Ok here"))
.collect();
```

can be replaced with a judicious use of [.filter_map()](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.filter_map):

```rust
let numeric_lines = reader.lines()
.filter_map(|line| line.ok())
.filter_map(|s| s.parse().ok())
//...
```rust [], ignore
let numbers: Vec<_> = ["1", "2", "3"]
.iter()
.filter_map(|s| s.parse::<i32>().ok())
.collect();
```

You will relive similar experiences when learning Rust without knowing the right tools from the standard library that let you convert `Result` into what you actually need.
Expand Down

0 comments on commit 85afd21

Please sign in to comment.