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

Update option_result.md #1864

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/error/multiple_error_types/option_result.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fn main() {
```

There are times when we'll want to stop processing on errors (like with
[`?`][enter_question_mark]) but keep going when the `Option` is `None`. A
couple of combinators come in handy to swap the `Result` and `Option`.
[`?`][enter_question_mark]) but keep going when the `Option` is `None`. The `transpose` function comes in handy to swap the `Result` and `Option`.

```rust,editable
use std::num::ParseIntError;
Expand All @@ -39,7 +38,7 @@ fn double_first(vec: Vec<&str>) -> Result<Option<i32>, ParseIntError> {
first.parse::<i32>().map(|n| 2 * n)
});

opt.map_or(Ok(None), |r| r.map(Some))
opt.transpose()
}

fn main() {
Expand Down
Loading