From 48bed57e671fcb937b30efc8be28f1f3696b8b1a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Wed, 12 Oct 2016 10:37:22 -0400 Subject: [PATCH] Use `try!` for the illustration of calling it in `main` Until rust-lang/rust#35946 is fixed, so that the error message is clearer than not implementing the Carrier trait. --- src/ch09-02-recoverable-errors-with-result.md | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/ch09-02-recoverable-errors-with-result.md b/src/ch09-02-recoverable-errors-with-result.md index e9c85a8b1..c027947fd 100644 --- a/src/ch09-02-recoverable-errors-with-result.md +++ b/src/ch09-02-recoverable-errors-with-result.md @@ -218,34 +218,30 @@ fn read_username_from_file() -> Result { Much nicer, right? The `try!` macro and the `?` operator make propagating errors upwards much more ergonomic. There's one catch though: they can only be used in functions that return a `Result`, since they expand to the same `match` -expression we saw above that had a potential early return of an `Err` value. Let's look at what happens if we try to use `?` in the `main` function, which has a return type of `()`: +expression we saw above that had a potential early return of an `Err` value. +Let's look at what happens if we try to use `try!` in the `main` function, +which you'll recall has a return type of `()`: ```rust,ignore -#![feature(question_mark)] # use std::fs::File; fn main() { - let f = File::open("hello.txt")?; + let f = try!(File::open("hello.txt")); } ``` + + When we compile this, we get the following error message: ```bash -``` - - - - -There's one problem though; let's try compiling the example: - -```rust,ignore - Compiling result v0.1.0 (file:///projects/result) error[E0308]: mismatched types - --> src/main.rs:6:13 + --> | -6 | let f = File::open("hello.txt")?; - | ^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum -`std::result::Result` +3 | let f = try!(File::open("hello.txt")); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::result::Result` | = note: expected type `()` = note: found type `std::result::Result<_, _>`