I often find myself tired of typing
let something = match something {
Ok(ok) => ok,
Err(_) => {
eprintln!("Oh no, something failed D:");
eprintln!("Try taking a glass of water and trying again!");
return;
}
};
.
I love match in general, but for errors it’s just so much boilerplate for so little.
I often just make an attempt macro:
macro_rules! attempt_or {
($result:expr, $fail:block) => {
match $result {
Ok(ok) => ok,
Err(_) => {
$fail
}
}
}
}
let something = attempt_or!(something, {
eprintln!("Oh no, something failed D:");
eprintln!("Try taking a glass of water and trying again!");
return;
});
This works, but it’s boring to rewrite that macro for every single project. Also, it doesn’t give us access to the err variable.
It would be absolutely amazing to have a macro like this (but better) built in to the STD, for simply extracting a value while still giving complete control over the error.
Thoughts?
EDIT: Another problem with this macro is that you have to return or break or similar, otherwise it complains about incompatible match types.