Skip to content

Commit

Permalink
Talk about options in ? chapter (#600)
Browse files Browse the repository at this point in the history
* Talk about options in `?` chapter

* Combine Option and Result examples

* Update try-operator.md

* Remove semicolon from expansion

* Focus on expansion of `expr?`

* Update try-operator.md

* Update try-operator.md

---------

Co-authored-by: Martin Geisler <mgeisler@google.com>
  • Loading branch information
sakex and mgeisler authored Jul 28, 2023
1 parent a7a9ee8 commit 98be9e1
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/error-handling/try-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,12 @@ Key points:

* The `username` variable can be either `Ok(string)` or `Err(error)`.
* Use the `fs::write` call to test out the different scenarios: no file, empty file, file with username.
* The return type of the function has to be compatible with the nested functions it calls. For instance,
a function returning a `Result<T, Err>` can only apply the `?` operator on a function returning a
`Result<AnyT, Err>`. It cannot apply the `?` operator on a function returning a `Result<T, OtherErr>`

This comment has been minimized.

Copy link
@domenukk

domenukk Aug 2, 2023

Collaborator

Wont ? work fine if OtherErr implements From<Err>?

or an `Option<AnyT>`. Reciprocally, a function returning an `Option<T>` can only apply the `?` operator
on a function returning an `Option<AnyT>`.
* You can convert incompatible types into one another with the different `Option` and `Result` methods
such as `Option::ok_or`, `Result::ok`, `Result::err`.

</details>

0 comments on commit 98be9e1

Please sign in to comment.