This is a really simple silly thing I just realized, but I noticed I have a lot code that looks something like this:
fn foo() -> Result<(), Error> {
// do something
}
fn bar() -> Option<()> {
let Ok(f) = foo() else {
return None;
};
}
I hated that if-statement. I realized today that I could simplify to:
fn bar() -> Option<()> {
let f = foo().ok()?;
}
And that cleaned up my code a lot. It’s a tiny thing, but when it’s okay to discard the error from the result, makes such a big difference when you have a lot of them!
I don’t get it!
If the
T
inResult
(and presumablyOption
) is()
, then why not just useis_ok()
/is_err()
from the caller side?Other Tips:
bool
hasthen()
andthen_some()
methods instd
now.transpose()
in both directions betweenResultᐸOptionᐸTᐳᐳ
s andOptionᐸResultᐸTᐳᐳ
s