1
Fork 0

Rollup merge of #39756 - JordiPolo:feature/try_to_question, r=steveklabnik

Sustitutes try! for ? in the Result documentation

Hopefully newcomers will go strait to use `?`
This commit is contained in:
Guillaume Gomez 2017-02-12 19:16:33 +01:00 committed by GitHub
commit fd006ad5a5

View file

@ -139,7 +139,7 @@
//! assert!(file.write_all(b"important message").is_ok()); //! 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::fs::File;
@ -147,17 +147,17 @@
//! # use std::io; //! # use std::io;
//! # #[allow(dead_code)] //! # #[allow(dead_code)]
//! fn write_message() -> io::Result<()> { //! fn write_message() -> io::Result<()> {
//! let mut file = try!(File::create("valuable_data.txt")); //! let mut file = File::create("valuable_data.txt")?;
//! try!(file.write_all(b"important message")); //! file.write_all(b"important message")?;
//! Ok(()) //! Ok(())
//! } //! }
//! ``` //! ```
//! //!
//! # The `try!` macro //! # The `?` syntax
//! //!
//! When writing code that calls many functions that return the //! When writing code that calls many functions that return the
//! [`Result`] type, the error handling can be tedious. The [`try!`] //! [`Result`] type, the error handling can be tedious. The [`?`]
//! macro hides some of the boilerplate of propagating errors up the //! syntax hides some of the boilerplate of propagating errors up the
//! call stack. //! call stack.
//! //!
//! It replaces this: //! It replaces this:
@ -208,37 +208,29 @@
//! } //! }
//! //!
//! fn write_info(info: &Info) -> io::Result<()> { //! 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 //! // Early return on error
//! try!(file.write_all(format!("name: {}\n", info.name).as_bytes())); //! file.write_all(format!("name: {}\n", info.name).as_bytes())?;
//! try!(file.write_all(format!("age: {}\n", info.age).as_bytes())); //! 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!("rating: {}\n", info.rating).as_bytes())?;
//! Ok(()) //! Ok(())
//! } //! }
//! ``` //! ```
//! //!
//! *It's much nicer!* //! *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 //! success ([`Ok`]) value, unless the result is [`Err`], in which case
//! [`Err`] is returned early from the enclosing function. Its simple definition //! [`Err`] is returned early from the enclosing function.
//! makes it clear:
//! //!
//! ``` //! [`?`] can only be used in functions that return [`Result`] because of the
//! macro_rules! try { //! early return of [`Err`] that it provides.
//! ($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.
//! //!
//! [`expect`]: enum.Result.html#method.expect //! [`expect`]: enum.Result.html#method.expect
//! [`Write`]: ../../std/io/trait.Write.html //! [`Write`]: ../../std/io/trait.Write.html
//! [`write_all`]: ../../std/io/trait.Write.html#method.write_all //! [`write_all`]: ../../std/io/trait.Write.html#method.write_all
//! [`io::Result`]: ../../std/io/type.Result.html //! [`io::Result`]: ../../std/io/type.Result.html
//! [`try!`]: ../../std/macro.try.html //! [`?`]: ../../std/macro.try.html
//! [`Result`]: enum.Result.html //! [`Result`]: enum.Result.html
//! [`Ok(T)`]: enum.Result.html#variant.Ok //! [`Ok(T)`]: enum.Result.html#variant.Ok
//! [`Err(E)`]: enum.Result.html#variant.Err //! [`Err(E)`]: enum.Result.html#variant.Err