@@ -189,10 +189,19 @@ macro_rules! debug_assert_eq {
189
189
( $( $arg: tt) * ) => ( if cfg!( debug_assertions) { assert_eq!( $( $arg) * ) ; } )
190
190
}
191
191
192
- /// Helper macro for unwrapping `Result` values while returning early with an
193
- /// error if the value of the expression is `Err`. Can only be used in
194
- /// functions that return `Result` because of the early return of `Err` that
195
- /// it provides.
192
+ /// Helper macro for reducing boilerplate code for matching `Result` together
193
+ /// with converting downstream errors.
194
+ ///
195
+ /// `try!` matches the given `Result`. In case of the `Ok` variant, the
196
+ /// expression has the value of the wrapped value.
197
+ ///
198
+ /// In case of the `Err` variant, it retrieves the inner error. `try!` then
199
+ /// performs conversion using `From`. This provides automatic conversion
200
+ /// between specialized errors and more general ones. The resulting
201
+ /// error is then immediately returned.
202
+ ///
203
+ /// Because of the early return, `try!` can only be used in functions that
204
+ /// return `Result`.
196
205
///
197
206
/// # Examples
198
207
///
@@ -201,18 +210,28 @@ macro_rules! debug_assert_eq {
201
210
/// use std::fs::File;
202
211
/// use std::io::prelude::*;
203
212
///
204
- /// fn write_to_file_using_try() -> Result<(), io::Error> {
213
+ /// enum MyError {
214
+ /// FileWriteError
215
+ /// }
216
+ ///
217
+ /// impl From<io::Error> for MyError {
218
+ /// fn from(e: io::Error) -> MyError {
219
+ /// MyError::FileWriteError
220
+ /// }
221
+ /// }
222
+ ///
223
+ /// fn write_to_file_using_try() -> Result<(), MyError> {
205
224
/// let mut file = try!(File::create("my_best_friends.txt"));
206
225
/// try!(file.write_all(b"This is a list of my best friends."));
207
226
/// println!("I wrote to the file");
208
227
/// Ok(())
209
228
/// }
210
229
/// // This is equivalent to:
211
- /// fn write_to_file_using_match() -> Result<(), io::Error > {
230
+ /// fn write_to_file_using_match() -> Result<(), MyError > {
212
231
/// let mut file = try!(File::create("my_best_friends.txt"));
213
232
/// match file.write_all(b"This is a list of my best friends.") {
214
233
/// Ok(v) => v,
215
- /// Err(e) => return Err(e ),
234
+ /// Err(e) => return Err(From::from(e) ),
216
235
/// }
217
236
/// println!("I wrote to the file");
218
237
/// Ok(())
0 commit comments