Rust Error Handling

Error handling is hard rusts error handling is substantially different from other languages. For a quick comparison let’s look at a python example: try: # code that fails except: # what happens when code fails This approach, although intuitive, leaves some important flaws. Namely: Unhandled errors may occur Errors are propagated implicitly, not explicitly, which can lead to some unwanted behavior If your try-block ends up containing multiple parts of code that could fail you have to handle each of them separately How rust handles errors If you have used rust before you’ll already know what comes up. The two main types for error handling in rust are Result and Option. The latter technically doesn’t count as an error type as it just represents the case where an operation may return nothing (i.e. NULL in C, None in python, etc.). The real error type is Result. ...

May 5, 2024 · 7 min · Moritz Sokoll