1.4 KiB
1.4 KiB
id, type, title, created, modified, tags, status, relates_to, depends_on, project
| id | type | title | created | modified | tags | status | relates_to | depends_on | project | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 550e8400-e29b-41d4-a716-446655440000 | note | Example Note - Rust Error Handling | 2026-01-17T10:30:00Z | 2026-01-17T10:35:00Z |
|
active |
|
|
knowledge-base |
Example Note - Rust Error Handling
This is an example of a note document generated from the note.md.tera template.
Overview
Rust error handling uses the Result<T, E> type for recoverable errors and panic! for unrecoverable errors.
Key Points
- Always use
Result<T>for operations that can fail - Use the
?operator for error propagation - Create custom error types with
thiserror - Provide context with error messages
Best Practices
-
Never use
unwrap()in production code- Use
?operator instead - Or use
unwrap_or(),unwrap_or_else()with defaults
- Use
-
Define clear error types
#[derive(Debug, thiserror::Error)] pub enum MyError { #[error("IO error: {0}")] Io(#[from] std::io::Error), #[error("Parse error: {0}")] Parse(String), } -
Provide helpful error messages
let config = load_config() .map_err(|e| format!("Failed to load config from {}: {}", path, e))?;