Member-only story

All about Error Handling In Rust — With Examples, Define custom errors and Boxed errors with Box<dyn Error>

Shanmukh Sista
9 min readApr 25, 2023
Photo by Tonik on Unsplash

When I started learning Rust lang, the first roadblock was it’s memory management and ownership rules. The next one that really got to me was error handling. As developers, at some point in writing a program, it becomes essential to handle all kinds of errors, and deal with them in usual program flow.

It took me a while to understand errors and result types in rust. In this article, I'm going to share working examples on how to handle errors, and how can we work with error handling in a nice clean manner with Rust custom error types.

I encourage readers to try this out in their developer environments and follow along. This really helps in understanding these concepts.

Recoverable and Unrecoverable Errors

Like many other programming languages, there are two categories of errors in Rust. Recoverable and Unrecoverable errors. Recoverable errors allow us the ability to act and handle any unexpected scenarios in the program gracefully — in case something goes wrong during normal flow of the program. Unrecoverable errors , on the other hand involve terminating the program immediately without giving a chance to handle them. An example of this would be panics and something…

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

Write a response