I would like to do this:
fn foo(bar: Option<...>) -> ... {
if let None = bar { // bar is None. No need to go any further
return ...
}
/* Do some complex calculation assuming bar is Some(...) */
}
This kind of approach is fine in untyped languages and even some typed ones like TypeScript and (if memory serves me right) Swift but it won't do for Rust.
What is the idiomatic way to do this that works for return types other than Option
and Result
? I could do:
if let Some(...) = bar {
/* complex calculation */
} else {
return ...
}
It seems akin to a lot of unreadable nested if-else clauses with a lot of indentation in the long run.