Designed by Martin Stoleru. Developed by Alinus Dumitrana

All Articles

Error Handling Patterns in Rust

Exploring idiomatic error handling in Rust — from Result types to custom error enums and the ? operator.

Written By Alinus Dumitrana

Error handling is one of Rust's strongest features. Instead of exceptions, Rust uses the Result type to make error paths explicit and impossible to ignore.

The Basics

Every fallible operation returns a Result<T, E>:

fn parse_config(path: &str) -> Result<Config, ConfigError> {
    let raw = std::fs::read_to_string(path)?;
    let config: Config = serde_yaml::from_str(&raw)?;
    Ok(config)
}

Custom Error Types

For libraries and larger applications, defining custom error types gives callers clear information about what went wrong:

enum AppError {
    NotFound(String),
    ParseError(String),
    Internal(String),
}

The ? Operator

The question mark operator is syntactic sugar for early returns on errors. It makes the happy path clean and readable while still handling every error case.

When to Use unwrap

In production code, unwrap() should be reserved for cases where a failure is truly impossible — like parsing a known-valid constant. For everything else, propagate the error or provide a meaningful default.

Other articles

Feb 04, 2026

Hello World

My first blog post about building a personal website with Rust and Axum.