Expand description
§Error Handling - Rust Book Chapter 9
This module demonstrates custom error types and error handling patterns from The Rust Book Chapter 9.
§Key Concepts Demonstrated
-
Custom Error Types with
thiserror(Chapter 9.2)- Using enums to represent different error conditions
- Adding context to errors with struct variants
- Automatic
Displayimplementation via#[error(...)]
-
Automatic Error Conversion with
#[from](Chapter 9.2)- The
#[from]attribute implementsFrom<OtherError>automatically - Enables using
?operator with different error types
- The
-
Type Alias for Results (Chapter 9.2)
- Using
type Result<T> = std::result::Result<T, SearchError> - Makes function signatures cleaner
- Using
-
Builder Methods with
impl Into<T>(Chapter 10.2)- Accepting flexible input types that can convert to the target type
- Makes APIs more ergonomic
§Learning Notes
The thiserror crate is industry standard for libraries because it:
- Derives
std::error::Errorautomatically - Generates helpful
Displaymessages - Integrates seamlessly with the
?operator
Compare this to the book’s manual error implementations to see how derive macros reduce boilerplate while maintaining full functionality.
Enums§
- Search
Error - Custom error type for code search operations.
Type Aliases§
- Result
- Result type alias for SearchError