mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-20 21:18:26 +02:00
Talk about options in ?
chapter (#600)
* 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>
This commit is contained in:
parent
a7a9ee84b4
commit
98be9e149c
@ -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>`
|
||||
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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user