diff --git a/nostarch/chapter-09-error-message-updates.md b/nostarch/chapter-09-error-message-updates.md index 0a3f498e0..51023b699 100644 --- a/nostarch/chapter-09-error-message-updates.md +++ b/nostarch/chapter-09-error-message-updates.md @@ -12,3 +12,32 @@ error[E0277]: the `?` operator can only be used in a function that returns = help: the trait `std::ops::Try` is not implemented for `()` = note: required by `std::ops::Try::from_error` ``` + +--- + +Then, please replace the highlighted paragraph on page 161 with this text: + +This error points out that we’re only allowed to use the `?` operator in a +function that returns `Result`. In functions that don’t return `Result`, when you call other functions that return `Result`, you’ll need to +use a `match` or one of the `Result` methods to handle the `Result` +instead of using the `?` operator to potentially propagate the error to the +calling code. + +However, we can change how we write the `main` function so that it does return +a `Result`: + +``` +use std::error::Error; +use std::fs::File; + +fn main() -> Result<(), Box> { + let f = File::open("hello.txt")?; + + Ok(()) +} +``` + +The `Box` type is called a “trait object,” which we’ll talk about in +the “Using Trait Objects that Allow for Values of Different Types” section of +Chapter 17. For now, you can read `Box` to mean “any kind of error.” diff --git a/src/ch09-03-to-panic-or-not-to-panic.md b/src/ch09-03-to-panic-or-not-to-panic.md index c8de1f08b..5d8878be8 100644 --- a/src/ch09-03-to-panic-or-not-to-panic.md +++ b/src/ch09-03-to-panic-or-not-to-panic.md @@ -81,7 +81,7 @@ bug in their code so they can fix it during development. Similarly, `panic!` is often appropriate if you’re calling external code that is out of your control and it returns an invalid state that you have no way of fixing. -However, when failure is expected, it is more appropriate to return a `Result` +However, when failure is expected, it's more appropriate to return a `Result` than to make a `panic!` call. Examples include a parser being given malformed data or an HTTP request returning a status that indicates you have hit a rate limit. In these cases, returning a `Result` indicates that failure is an