Skip to content

Instantly share code, notes, and snippets.

@tomjakubowski
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjakubowski/2712de6b48b7a5a5cad1 to your computer and use it in GitHub Desktop.
Save tomjakubowski/2712de6b48b7a5a5cad1 to your computer and use it in GitHub Desktop.
#![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