Rust Error Reference

Search Rust standard-library error types, traits and common crate error variants.

A searchable reference for Rust's std::error::Error trait, io::ErrorKind variants, parse error types, and the Result/Option helpers plus anyhow and thiserror conventions. Runs in your browser.

What does the ? operator do in Rust?

The ? operator unwraps an Ok value or returns early on Err, automatically applying the From trait to convert the error into the function's declared error type. It is the idiomatic way to propagate failures without manual match statements.

This is a searchable reference for Rust error handling — the std::error::Error trait, the Result/Option enums, the io::ErrorKind variants, the standard parse error types (ParseIntError, Utf8Error), and the conventions of the widely used anyhow and thiserror crates.

How it works

Rust has no exceptions. Fallible operations return Result<T, E> (either Ok(T) or Err(E)), and absence is modelled with Option<T>. The std::error::Error trait unifies error values and exposes source() to walk a cause chain. Concrete errors like io::Error carry an ErrorKind enum — NotFound, PermissionDenied, TimedOut — so you can match on the category portably with err.kind() instead of inspecting raw OS numbers.

You propagate failures with the ? operator. Applied to a Result, it returns early on Err, automatically calling From to convert that error into the function’s declared error type. Libraries typically define their own error enum (often derived with thiserror and #[from] for those conversions), while applications box everything behind anyhow::Error and attach human-readable context().

Example

Parse a number, propagate the error, then inspect an I/O kind:

use std::io::{self, ErrorKind};

fn read_port(s: &str) -> Result<u16, Box<dyn std::error::Error>> {
    let n: u16 = s.trim().parse()?;   // ? converts ParseIntError via From
    Ok(n)
}

match std::fs::read("config.toml") {
    Ok(bytes) => { /* ... */ }
    Err(e) if e.kind() == ErrorKind::NotFound => { /* use defaults */ }
    Err(e) => return Err(e.into()),
}

Reach for unwrap() only when failure is truly impossible — otherwise prefer ?, which keeps the program recoverable instead of panicking. Everything runs in your browser; nothing is uploaded.