Skip to content

Commit

Permalink
Sustitutes try! for ? in the Result documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiPolo committed Feb 12, 2017
1 parent ba7cf7c commit 6b5c5f2
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,25 @@
//! assert!(file.write_all(b"important message").is_ok());
//! ```
//!
//! Or propagate the error up the call stack with [`try!`]:
//! Or propagate the error up the call stack with [`?`]:
//!
//! ```
//! # use std::fs::File;
//! # use std::io::prelude::*;
//! # use std::io;
//! # #[allow(dead_code)]
//! fn write_message() -> io::Result<()> {
//! let mut file = try!(File::create("valuable_data.txt"));
//! try!(file.write_all(b"important message"));
//! let mut file = File::create("valuable_data.txt")?;
//! file.write_all(b"important message")?;
//! Ok(())
//! }
//! ```
//!
//! # The `try!` macro
//! # The `?` syntax
//!
//! When writing code that calls many functions that return the
//! [`Result`] type, the error handling can be tedious. The [`try!`]
//! macro hides some of the boilerplate of propagating errors up the
//! [`Result`] type, the error handling can be tedious. The [`?`]
//! syntax hides some of the boilerplate of propagating errors up the
//! call stack.
//!
//! It replaces this:
Expand Down Expand Up @@ -208,37 +208,29 @@
//! }
//!
//! fn write_info(info: &Info) -> io::Result<()> {
//! let mut file = try!(File::create("my_best_friends.txt"));
//! let mut file = File::create("my_best_friends.txt")?;
//! // Early return on error
//! try!(file.write_all(format!("name: {}\n", info.name).as_bytes()));
//! try!(file.write_all(format!("age: {}\n", info.age).as_bytes()));
//! try!(file.write_all(format!("rating: {}\n", info.rating).as_bytes()));
//! file.write_all(format!("name: {}\n", info.name).as_bytes())?;
//! file.write_all(format!("age: {}\n", info.age).as_bytes())?;
//! file.write_all(format!("rating: {}\n", info.rating).as_bytes())?;
//! Ok(())
//! }
//! ```
//!
//! *It's much nicer!*
//!
//! Wrapping an expression in [`try!`] will result in the unwrapped
//! Ending the expression with [`?`] will result in the unwrapped
//! success ([`Ok`]) value, unless the result is [`Err`], in which case
//! [`Err`] is returned early from the enclosing function. Its simple definition
//! makes it clear:
//! [`Err`] is returned early from the enclosing function.
//!
//! ```
//! macro_rules! try {
//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
//! }
//! ```
//!
//! [`try!`] is imported by the prelude and is available everywhere, but it can only
//! be used in functions that return [`Result`] because of the early return of
//! [`Err`] that it provides.
//! [`?`] can only be used in functions that return [`Result`] because of the
//! early return of [`Err`] that it provides.
//!
//! [`expect`]: enum.Result.html#method.expect
//! [`Write`]: ../../std/io/trait.Write.html
//! [`write_all`]: ../../std/io/trait.Write.html#method.write_all
//! [`io::Result`]: ../../std/io/type.Result.html
//! [`try!`]: ../../std/macro.try.html
//! [`?`]: ../../std/macro.try.html
//! [`Result`]: enum.Result.html
//! [`Ok(T)`]: enum.Result.html#variant.Ok
//! [`Err(E)`]: enum.Result.html#variant.Err
Expand Down

0 comments on commit 6b5c5f2

Please sign in to comment.