3 ms·
for exploratory coding you can also just unwrap, assert, panic.. It's useful to remember that error handling is optional in those cases :)
by egnehots 6y ago
for exploratory coding you can also just unwrap, assert, panic..
It's useful to remember that error handling is optional in those cases :)
- unrealhoang 6y agoanyhow 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.
- edflsafoiewq 6y ago? 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.
- rklaehn 6y agoI somehow still prefer ?, even in places where unwrap would be perfectly fine, like tests. unwrap is somehow unpleasant to look at. 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.