Rust: Result, Traits and Derivations
Error Handling in Rust
- Rust distinguishes between recoverable and unrecoverable errors.
- Recoverable errors: handled with
Result<T, E>. These can be predicted and recovered from. - Unrecoverable errors: handled with the
panic!macro. These should stop the program to prevent further issues.
Unrecoverable Errors with panic!
panic!terminates the program and displays an error message.- Use
panic!for:- Invalid program state (e.g., negative health).
- Critical resource failures (e.g., memory allocation).
- Bugs or logical errors.
- Can configure to
aborton panic instead of unwinding the stack to reduce binary size.
Recoverable Errors with Result<T, E>
Result<T, E>enum represents either success (Ok(T)) or failure (Err(E)).T: Type of the value returned on success.E: Type of the error returned on failure.- Handle
ResultwithmatchforOkandErrcases. - Use the
?operator to propagate errors to the caller. unwrapreturns the value insideOkor panics if it’sErr.expectis likeunwrapbut allows a custom panic message.and_thenchains operations that returnResult; any failure stops the chain.- Use
Resultfor recoverable errors like file I/O, network requests, or user input validation.
Custom Error Types
Custom error types provide better error handling and context.
Implemented using
enumsorstructs.Example (enum):
#[derive(Debug)] enum MyError { FileNotFound, PermissionDenied, InvalidData, }Example (struct):
#[derive(Debug)] struct ParseError { message: String, line: u32, column: u32, }
Traits
Traits define shared behavior across types, enabling code reuse and polymorphism.
Define method signatures that implementing types must provide.
Traits can provide default implementations.
Example:
pub trait Summary { fn summarize(&self) -> String; }Use
impl Traitsyntax for accepting or returning types that implement a trait.Trait bounds (
<T: Summary>) specify that a generic type must implement a trait.Multiple trait bounds can be combined using
+.
The Error Trait
- Implementing the
Errortrait (fromstd::error::Error) allows custom errors to be compatible with libraries expectingErrortypes. - Also implement
fmt::Displayfor a human-readable description.