Intro
You’ll run into errors eventually, it’s just how it is. I often find that what makes one language more enjoyable than another one to work with is how it handles errors. Rust’s approach is very interesting (i ran into this in my first project before reaching this chapter) and I can only compare it to Golang in that regard.
There are Two Categories:
- Recoverable → informs, logs, retries, etc.
- Unrecoverable → something went wrong, stop the program.
Rust’s Errors
Rust doesn’t treat the two categories the same and offers different mechanisms, and has no Exceptions.
Instead, it has the type Result<T, E> for recoverable errors and the panic! macro that stops execution.
Unrecoverable Errors with panic!
Program’s panic when a certain action causes it to panic, for example accessing an array out of bounds. We can also explicitly cause a program to panic using the panic! macro.
When a program panics it prints a failure message, cleans up the stack, and quits. In the printed message is the call stack, so we can track the error’s source.