Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sustitutes try! for ? in the Result documentation #39756

Merged
merged 1 commit into from
Feb 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be changed to point to documentation for ? rather than the try macro.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the try doc explains about ? and it being the preferred way, I am not sure if there is a document for ?.

//! [`Result`]: enum.Result.html
//! [`Ok(T)`]: enum.Result.html#variant.Ok
//! [`Err(E)`]: enum.Result.html#variant.Err
Expand Down