Skip to content

Commit aa339f7

Browse files
authored
Merge pull request #2047 from UnHumbleBen/patch-5
Subtle fix to introduce ? on Option in Chapter 9.2
2 parents 34b4038 + 239d99f commit aa339f7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/ch09-02-recoverable-errors-with-result.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,13 @@ and returns it. Of course, using `fs::read_to_string` doesn’t give us the
470470
opportunity to explain all the error handling, so we did it the longer way
471471
first.
472472

473-
#### The `?` Operator Can Only Be Used in Functions That Return `Result`
473+
#### The `?` Operator Can Be Used in Functions That Return `Result`
474474

475-
The `?` operator can only be used in functions that have a return type of
475+
The `?` operator can be used in functions that have a return type of
476476
`Result`, because it is defined to work in the same way as the `match`
477477
expression we defined in Listing 9-6. The part of the `match` that requires a
478478
return type of `Result` is `return Err(e)`, so the return type of the function
479-
must be a `Result` to be compatible with this `return`.
479+
can be a `Result` to be compatible with this `return`.
480480

481481
Let’s look at what happens if we use the `?` operator in the `main` function,
482482
which you’ll recall has a return type of `()`:
@@ -505,8 +505,9 @@ error[E0277]: the `?` operator can only be used in a function that returns
505505
```
506506

507507
This error points out that we’re only allowed to use the `?` operator in a
508-
function that returns `Result<T, E>`. When you’re writing code in a function
509-
that doesn’t return `Result<T, E>`, and you want to use `?` when you call other
508+
function that returns `Result` or `Option` or another type that implements
509+
`std::ops::Try`. When you’re writing code in a function
510+
that doesn’t return one of these types, and you want to use `?` when you call other
510511
functions that return `Result<T, E>`, you have two choices to fix this problem.
511512
One technique is to change the return type of your function to be `Result<T,
512513
E>` if you have no restrictions preventing that. The other technique is to use

0 commit comments

Comments
 (0)