Rust Error Handling Patterns
Category: Tutorials
⚠️ PLACEHOLDER CONTENT: This is a fake blog post with dummy content for demonstration purposes only.
The ? Operator
Rust’s ? operator makes error propagation elegant:
use std::fs::File;
use std::io::{self, Read};
fn read_config() -> io::Result<String> {
let mut file = File::open("config.toml")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
Custom Error Types
Define your own errors with thiserror:
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AppError {
#[error("Configuration file not found")]
ConfigNotFound,
#[error("Invalid data: {0}")]
InvalidData(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
fn load_config() -> Result<Config, AppError> {
let data = std::fs::read_to_string("config.toml")
.map_err(|_| AppError::ConfigNotFound)?;
parse_config(&data)
.ok_or_else(|| AppError::InvalidData("malformed TOML".into()))
}
The anyhow Crate
For applications (not libraries), anyhow simplifies everything:
use anyhow::{Context, Result};
fn main() -> Result<()> {
let config = load_config()
.context("Failed to load application config")?;
run_app(config)?;
Ok(())
}
This post is placeholder content and does not represent real technical advice.