I would like to preface this with a warning: that I don’t really expect any of this to come to direct fruition because both the syntax is terribly unwieldy and because it isn’t really a “problem” and more of difference in programming styles and could just be answered with “well this is the Rust way”.
With that being said here is the problem statement:
Early Returns:
I have generally been both taught and told that it is generally good practice to check errors and then early return as quickly as possible (so as to not do unnecessary work before reporting errors that were known before hand).
This is generally tied with a programming style that has the following sort of format:
{
if var.field1.is_some_err() {
return Err(some_err);
}
if var.field2.is_some_other_err() {
return Err(some_other_err);
}
// Rest of the method follows
...
}
I find that this coding style is easy to both read and understand. However, if we add if let binds to the mix this is no longer possible, because the bindings are bound for the inner scope of the if statement and the error must then be at the bottom of the if in the else statement. This both separates the error returns and indents the code even further.
So I have thought, for some time, if there will ever be a way to “flatten” rust code of if-let statements.
Maybe something like the following (very-bikesheddy)
{
let bind_name;
if !(let Some(@ bind_name) = &fn_call()) {
return Err(some_err);
}
// bind_name is bound to a ref of the inner value returned by fn_call()
...
}