I find that Rust tends to have code that goes sideways more than downward. I prefer the latter and most C code bases, that I find elegant are like that.
It is like that, because of all the chaining that one can do. It is also just a feeling.
It is like that, because of all the chaining that one can do. It is also just a feeling.
Sometimes it's also the formatter though that outputs intensely sideways drifting code: https://github.com/rust-lang/rust/blob/1.60.0/compiler/rustc...
To counteract it, I write exit-early code like this:
let foo_result = foo();
if let Err(e) = foo_result {
return Bar::Fail(e);
}
let foo_result = foo_result.unwrap();
... let foo_result = foo()
.map_err(Bar::Fail)?;You can write it like this:
let foo_result = match foo() {
Ok(v) => v,
Err(e) => return Bar::Fail(e)
}; let foo = match foo() {
Ok(foo) => foo,
Err(err) => return Err(err),
};
I was typing it out so often I made editor aliases `lmo` and `lmr` for Option and ResultThe code above can be written as:
let foo = foo()?; let Some(x) = foo() else { return 42 }; if let Ok(x) = my_func() {
// ...
}
Or do you mean something else?Of course, that's what goto is actually good for.