kogral/templates/examples/note-example.md
Jesús Pérez 9ea04852a8
Some checks failed
Rust CI / Security Audit (push) Has been cancelled
Rust CI / Check + Test + Lint (nightly) (push) Has been cancelled
Rust CI / Check + Test + Lint (stable) (push) Has been cancelled
Nickel Type Check / Nickel Type Checking (push) Has been cancelled
chore: add schemas and just recipes
2026-01-23 16:12:50 +00:00

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
rust
error-handling
best-practices
active
guideline-rust-errors
pattern-result-type
guideline-rust-basics
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

  1. Never use unwrap() in production code

    • Use ? operator instead
    • Or use unwrap_or(), unwrap_or_else() with defaults
  2. 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),
    }
    
  3. Provide helpful error messages

    let config = load_config()
        .map_err(|e| format!("Failed to load config from {}: {}", path, e))?;
    

References