|
139 | 139 | //! assert!(file.write_all(b"important message").is_ok());
|
140 | 140 | //! ```
|
141 | 141 | //!
|
142 |
| -//! Or propagate the error up the call stack with [`try!`]: |
| 142 | +//! Or propagate the error up the call stack with [`?`]: |
143 | 143 | //!
|
144 | 144 | //! ```
|
145 | 145 | //! # use std::fs::File;
|
146 | 146 | //! # use std::io::prelude::*;
|
147 | 147 | //! # use std::io;
|
148 | 148 | //! # #[allow(dead_code)]
|
149 | 149 | //! 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")?; |
152 | 152 | //! Ok(())
|
153 | 153 | //! }
|
154 | 154 | //! ```
|
155 | 155 | //!
|
156 |
| -//! # The `try!` macro |
| 156 | +//! # The `?` syntax |
157 | 157 | //!
|
158 | 158 | //! 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 |
161 | 161 | //! call stack.
|
162 | 162 | //!
|
163 | 163 | //! It replaces this:
|
|
208 | 208 | //! }
|
209 | 209 | //!
|
210 | 210 | //! 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")?; |
212 | 212 | //! // 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())?; |
216 | 216 | //! Ok(())
|
217 | 217 | //! }
|
218 | 218 | //! ```
|
219 | 219 | //!
|
220 | 220 | //! *It's much nicer!*
|
221 | 221 | //!
|
222 |
| -//! Wrapping an expression in [`try!`] will result in the unwrapped |
| 222 | +//! Ending the expression with [`?`] will result in the unwrapped |
223 | 223 | //! 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. |
226 | 225 | //!
|
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. |
236 | 228 | //!
|
237 | 229 | //! [`expect`]: enum.Result.html#method.expect
|
238 | 230 | //! [`Write`]: ../../std/io/trait.Write.html
|
239 | 231 | //! [`write_all`]: ../../std/io/trait.Write.html#method.write_all
|
240 | 232 | //! [`io::Result`]: ../../std/io/type.Result.html
|
241 |
| -//! [`try!`]: ../../std/macro.try.html |
| 233 | +//! [`?`]: ../../std/macro.try.html |
242 | 234 | //! [`Result`]: enum.Result.html
|
243 | 235 | //! [`Ok(T)`]: enum.Result.html#variant.Ok
|
244 | 236 | //! [`Err(E)`]: enum.Result.html#variant.Err
|
|
0 commit comments