Earlier quoted context omitted.
I found a crate that claims to do #1: https://github.com/dtolnay/no-panic This also looks interesting: https://github.com/Technolution/rustig
Yep, the no-panic crate is the hack I mentioned. It's using the linking process to fail compilation if I remember correctly. It only works on individual functions. rustig I didn't know and looks very interesting, thanks. Having it integrated in the compiler as annotations and guarantees would be ideal.
Where Everything Went Wrong: Error Handling and Error Messages in Rust (2020)
201–204 of 204 posts
Re: Where Everything Went Wrong: Error Handling and Error Messages in Rust (2020)
#202For the mostpart, Rust error handling is okay. What really rustles my jimmies, however, is the often mandatory indentation because of a lack of an inverse "if let". I prefer to bail out of a block if a condition is NOT met, rather than execute another nested block if it IS met. Rust makes that harder than it should be. It's good code hygiene in every other language, and Rust makes it painful in places. I've even been…
// want to do this
if not let Some("pattern") = val {
doSomething();
}
// can instead do
if !matches!(val, Some("pattern")) {
doSomething();
}
edit: formattingRe: Where Everything Went Wrong: Error Handling and Error Messages in Rust (2020)
#203Earlier quoted context omitted.
anyhow with ? is even shorter than unwrap. If it’s likely to have error at some point, I’d throw in a .context, it is so convenient. Edit: typo.
? requires the return type be annotated with an error type and the success case be annotated with Ok, for all functions up the stack, between the current function and where handling occurs. unwrap is purely local.
E.g. you can have tests that return an anyhow::Result, and use ? anywhere in the test. The test will fail if the result is not Ok(()).
Sure, you have the additional boilerplate of the return type annotation and the final Ok(()), but the test logic itself reads nicer, I think.
Re: Where Everything Went Wrong: Error Handling and Error Messages in Rust (2020)
#204Earlier quoted context omitted.
The "?" operator doesn't turn them into exceptions, it's just a "return-early-if-error" shortcut. The main difference being that the caller still has to handle the error of a function using "?". (even if it's by punting further up the call stack with more "?", which you could argue is exactly how exceptions work, but it is at least explicit in what functions can fail and which don't)
> even if it's by punting further up the call stack with more "?" Yes, this is exactly what I meant. > it is at least explicit in what functions can fail and which don't So is Java with exceptions (at least as long as developers are even slightly disciplined).