Last active
August 29, 2015 14:03
-
-
Save tomjakubowski/2712de6b48b7a5a5cad1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(macro_rules)] | |
macro_rules! try_harder { | |
($e:expr) => { | |
match $e { | |
Ok(val) => val, | |
Err(e) => return Err(e) | |
} | |
}; | |
($e:expr, $f:expr) => { | |
match $e.map_err($f) { | |
Ok(val) => val, | |
Err(e) => return Err(e) | |
} | |
} | |
} | |
fn baz() -> Result<i32, i64> { | |
Err(420) | |
} | |
fn bar() -> Result<i32, String> { | |
Ok(42) | |
} | |
fn foo() -> Result<i32, String> { | |
let m = try_harder!(bar()); | |
let n = try_harder!(baz(), |n| n.to_string()); | |
Ok(m + n) | |
} | |
fn main() { | |
println!("{}", foo()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment