Skip to content

Commit

Permalink
Rollup merge of rust-lang#36099 - skade:better-try-documentation, r=s…
Browse files Browse the repository at this point in the history
…teveklabnik

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.
  • Loading branch information
Jonathan Turner authored Sep 2, 2016
2 parents 8d4f1c1 + 0f8eb81 commit 0868aaa
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,19 @@ macro_rules! debug_assert_eq {
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
}

/// Helper macro for unwrapping `Result` values while returning early with an
/// error if the value of the expression is `Err`. Can only be used in
/// functions that return `Result` because of the early return of `Err` that
/// it provides.
/// Helper macro for reducing boilerplate code for matching `Result` together
/// with converting downstream errors.
///
/// `try!` matches the given `Result`. In case of the `Ok` variant, the
/// expression has the value of the wrapped value.
///
/// In case of the `Err` variant, it retrieves the inner error. `try!` then
/// performs conversion using `From`. This provides automatic conversion
/// between specialized errors and more general ones. The resulting
/// error is then immediately returned.
///
/// Because of the early return, `try!` can only be used in functions that
/// return `Result`.
///
/// # Examples
///
Expand All @@ -201,18 +210,28 @@ macro_rules! debug_assert_eq {
/// use std::fs::File;
/// use std::io::prelude::*;
///
/// fn write_to_file_using_try() -> Result<(), io::Error> {
/// enum MyError {
/// FileWriteError
/// }
///
/// impl From<io::Error> for MyError {
/// fn from(e: io::Error) -> MyError {
/// MyError::FileWriteError
/// }
/// }
///
/// fn write_to_file_using_try() -> Result<(), MyError> {
/// let mut file = try!(File::create("my_best_friends.txt"));
/// try!(file.write_all(b"This is a list of my best friends."));
/// println!("I wrote to the file");
/// Ok(())
/// }
/// // This is equivalent to:
/// fn write_to_file_using_match() -> Result<(), io::Error> {
/// fn write_to_file_using_match() -> Result<(), MyError> {
/// let mut file = try!(File::create("my_best_friends.txt"));
/// match file.write_all(b"This is a list of my best friends.") {
/// Ok(v) => v,
/// Err(e) => return Err(e),
/// Err(e) => return Err(From::from(e)),
/// }
/// println!("I wrote to the file");
/// Ok(())
Expand Down

0 comments on commit 0868aaa

Please sign in to comment.