Rust Error Tutorial

There's a lot of do_something.unwrap() floating around the Internet of Rust tutorials, so I got inspired to write a tutorial on error handling in Rust to supplement the really excellent chapter in the Rust book. I hope this helps someone and welcome any feedback/suggestions for improvements!


I wish I had read this six months ago, before I learned all this stuff myself the hard way! It's both concise and comprehensive!

One nit: in your downcasting example, you take it as a reference to whatever conrete error type the downcast was attempted to.

Thanks for your kind words and for pointing out the typo, which will promptly be fixed.

Just finished reading through, I found that really helpful thank you.

Will definitely start using BoxErrorand glad to see the error handling isn't as complicated as the multitude of error crates might suggest.

Thanks for writing this. I did see one thing worth clarifying, namely:

When you panic!() your program's stack is not unwound, objects' Drop traits are not invoked, and consequently resources are not cleaned up.

I’m pretty sure this is true in panic=abort mode, but false by default, so these “not”s should be “may not be”s (among other tweaks to the other panic-related sentences). And the section of the post about panicking should probably say explicitly that it’s a “may” because of these panic modes.


Typo in your paragraph on the "second bit of magic:" "The good new is ..."

And in the paragraph listing legitimate reasons to panic: "It's not the result of user error or a a faulty ..."

Thank you for reading! I have edited the section in question, hopefully to your satisfaction. You are right that panics do unwind by default, which is a great feature of the Rust language. I think you will still agree that one should not rely upon panics for graceful error handling when a Result<_,Error> can do the job more safely. I was hoping to convey this to the reader without getting too mired in technical details.


Yep, everything else seems totally correct to me, panics definitely should not be used when errors are potentially recoverable or unproblematic to propagate. The previous text just gave the very strong impression that Drop impls don’t get invoked on unwinding panics, which connects to all sorts of other persistent confusions we’ve seen about Rust drop()s (and C++ destructors). All the other reasons why drop()s might not get called are indeed irrelevant technical details for an error handling post and are correctly omitted.

1 Like