Macro show_notes::main_try [] [src]

macro_rules! main_try {
    ($e:expr) => { ... };
}

Define a macro like try! but which works in the context of main().

try! takes a Result<T, E> and, if it is Ok<T>, supplies the T value, or if it is Err<E> returns the error from the function. However, since main has a void tuple () return type, you cannot use try! in main(). This main_try! macro instead debug-prints the error and returns.

// Alias `Result` for brevity.
type LocalResult = Result<i32, &'static str>;

let an_ok: LocalResult = Ok(10);
let ok_val = main_try!(an_ok);
assert_eq!(ok_val, 10);

// We try to assign to another val, but it's an error, so we return.
let an_err: LocalResult = Err("Alas, this is a failure.");
let err_val = main_try!(an_err);  // Prints `Alas, this is a failure.`
// We'll never get here. If we did, the doctest would fail.
assert!(false);