Chapter 15: Error Handling with Result
Error handling is pivotal for building robust software. In C, developers often rely on return codes or global variables (such as errno
), which can be easy to ignore or mishandle. Rust offers a type-based approach that enforces explicit error handling by distinguishing between recoverable and unrecoverable errors at compile time.
When a function might fail in a way that your code can handle, it returns a Result
type. If the error cannot be reasonably resolved, Rust provides the panic!
macro to halt execution. This strong distinction prevents overlooked failures and promotes safety.