Skip to content

Commit fd006ad

Browse files
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 `?`
2 parents 461efc7 + 6b5c5f2 commit fd006ad

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/libcore/result.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,25 @@
139139
//! assert!(file.write_all(b"important message").is_ok());
140140
//! ```
141141
//!
142-
//! Or propagate the error up the call stack with [`try!`]:
142+
//! Or propagate the error up the call stack with [`?`]:
143143
//!
144144
//! ```
145145
//! # use std::fs::File;
146146
//! # use std::io::prelude::*;
147147
//! # use std::io;
148148
//! # #[allow(dead_code)]
149149
//! fn write_message() -> io::Result<()> {
150-
//! let mut file = try!(File::create("valuable_data.txt"));
151-
//! try!(file.write_all(b"important message"));
150+
//! let mut file = File::create("valuable_data.txt")?;
151+
//! file.write_all(b"important message")?;
152152
//! Ok(())
153153
//! }
154154
//! ```
155155
//!
156-
//! # The `try!` macro
156+
//! # The `?` syntax
157157
//!
158158
//! When writing code that calls many functions that return the
159-
//! [`Result`] type, the error handling can be tedious. The [`try!`]
160-
//! macro hides some of the boilerplate of propagating errors up the
159+
//! [`Result`] type, the error handling can be tedious. The [`?`]
160+
//! syntax hides some of the boilerplate of propagating errors up the
161161
//! call stack.
162162
//!
163163
//! It replaces this:
@@ -208,37 +208,29 @@
208208
//! }
209209
//!
210210
//! fn write_info(info: &Info) -> io::Result<()> {
211-
//! let mut file = try!(File::create("my_best_friends.txt"));
211+
//! let mut file = File::create("my_best_friends.txt")?;
212212
//! // Early return on error
213-
//! try!(file.write_all(format!("name: {}\n", info.name).as_bytes()));
214-
//! try!(file.write_all(format!("age: {}\n", info.age).as_bytes()));
215-
//! try!(file.write_all(format!("rating: {}\n", info.rating).as_bytes()));
213+
//! file.write_all(format!("name: {}\n", info.name).as_bytes())?;
214+
//! file.write_all(format!("age: {}\n", info.age).as_bytes())?;
215+
//! file.write_all(format!("rating: {}\n", info.rating).as_bytes())?;
216216
//! Ok(())
217217
//! }
218218
//! ```
219219
//!
220220
//! *It's much nicer!*
221221
//!
222-
//! Wrapping an expression in [`try!`] will result in the unwrapped
222+
//! Ending the expression with [`?`] will result in the unwrapped
223223
//! success ([`Ok`]) value, unless the result is [`Err`], in which case
224-
//! [`Err`] is returned early from the enclosing function. Its simple definition
225-
//! makes it clear:
224+
//! [`Err`] is returned early from the enclosing function.
226225
//!
227-
//! ```
228-
//! macro_rules! try {
229-
//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
230-
//! }
231-
//! ```
232-
//!
233-
//! [`try!`] is imported by the prelude and is available everywhere, but it can only
234-
//! be used in functions that return [`Result`] because of the early return of
235-
//! [`Err`] that it provides.
226+
//! [`?`] can only be used in functions that return [`Result`] because of the
227+
//! early return of [`Err`] that it provides.
236228
//!
237229
//! [`expect`]: enum.Result.html#method.expect
238230
//! [`Write`]: ../../std/io/trait.Write.html
239231
//! [`write_all`]: ../../std/io/trait.Write.html#method.write_all
240232
//! [`io::Result`]: ../../std/io/type.Result.html
241-
//! [`try!`]: ../../std/macro.try.html
233+
//! [`?`]: ../../std/macro.try.html
242234
//! [`Result`]: enum.Result.html
243235
//! [`Ok(T)`]: enum.Result.html#variant.Ok
244236
//! [`Err(E)`]: enum.Result.html#variant.Err

0 commit comments

Comments
 (0)