Skip to content

Commit 0868aaa

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36099 - skade:better-try-documentation, r=steveklabnik
Document try!'s error conversion behaviour try!'s documentation currently doesn't document the error conversion behaviour of the macro. This patch extends the documentation. Open questions: * is it worthwhile to have seperate examples with and without wrapping behaviour? It's not immediately obvious that From<T> for T is always defined. Though this is necessary for the macro to work in any case, is this the place to expect that knowledge.
2 parents 8d4f1c1 + 0f8eb81 commit 0868aaa

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/libcore/macros.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,19 @@ macro_rules! debug_assert_eq {
189189
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
190190
}
191191

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`.
196205
///
197206
/// # Examples
198207
///
@@ -201,18 +210,28 @@ macro_rules! debug_assert_eq {
201210
/// use std::fs::File;
202211
/// use std::io::prelude::*;
203212
///
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> {
205224
/// let mut file = try!(File::create("my_best_friends.txt"));
206225
/// try!(file.write_all(b"This is a list of my best friends."));
207226
/// println!("I wrote to the file");
208227
/// Ok(())
209228
/// }
210229
/// // This is equivalent to:
211-
/// fn write_to_file_using_match() -> Result<(), io::Error> {
230+
/// fn write_to_file_using_match() -> Result<(), MyError> {
212231
/// let mut file = try!(File::create("my_best_friends.txt"));
213232
/// match file.write_all(b"This is a list of my best friends.") {
214233
/// Ok(v) => v,
215-
/// Err(e) => return Err(e),
234+
/// Err(e) => return Err(From::from(e)),
216235
/// }
217236
/// println!("I wrote to the file");
218237
/// Ok(())

0 commit comments

Comments
 (0)