liveBook · Manning

Error

As explained in Rust in Action: Systems programming concepts and techniques

std::error::Error is Rust's standard trait for representing errors that can provide descriptions and optional source errors.

std::error::Error is Rust’s standard trait for errors that provide descriptions and can optionally expose source errors. It gives different error types a common interface, which is useful when one function must handle failures from several dependencies. section 8.5.2

A practical pattern is to define an enum containing each upstream error, derive Debug, implement Display, and then implement Error. The wrapper preserves the original error variants instead of erasing them behind Box<dyn Error>. section 8.5.2

Combining file and address-parsing errors

A function can return Result<(), UpstreamError> while opening a file and parsing "::1". With map_err(), each failure becomes IO or Parsing; with From implementations, both operations can use ?. A missing file then reports the preserved IO variant.

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Builds a custom error enum through Debug, Display, Error, map_err(), and From, but does not detail custom source-error methods.
Examples
Uses File::open("invisible.txt"), IPv6 parsing of "::1", UpstreamError variants, and displayed missing-file output.
What it assumes
Assumes familiarity with Result and dependency error types while introducing custom enums, Display, Error, map_err(), and From.
What you can do after
Build it from scratch by wrapping several dependency errors and propagating them through Result with map_err() or From.
How it connects
Connects Error with Result, trait objects, enums, Debug, Display, map_err(), From, and the question-mark operator.

Also explained in

Compare how 3 books explain Error →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.