BorrowCheck › Rust language core

Error handling

Option and Result, the ? operator, panic and unwinding, custom error types, Box<dyn Error>.

The topic explains error handling: recoverable errors as Option and Result values and unrecoverable ones as a panic. The questions check the combinators and bridges between Option and Result, the difference between map and and_then, the eager evaluation of the ok_or argument, the warning on an unused Result, and the Debug requirement of unwrap. The subtopic on the ? operator covers which types it applies to, why it returns from the nearest function or closure, and why it performs exactly one conversion through From. A separate part covers panics, unwinding and abort: destructors run during unwinding, a panic inside Drop, catch_unwind and UnwindSafe, #[track_caller], process exit codes, and the fact that process::exit runs no destructors. The last subtopics are about custom error types and Box<dyn Error>: why the Error trait requires Display, what source provides, how main prints a returned error, why Box<dyn Error> does not itself implement Error, and how to recover the concrete type with downcast_ref.

38 questions: 19 basic, 14 intermediate, 5 advanced. 5 info pages, 30 sources.

Subtopics

  1. Option and Result
  2. The ? operator and From
  3. Panic, unwind, abort
  4. Custom error types, the Error trait
  5. Box<dyn Error> and wrappers

Previous: Type conversions · Next: Modules, Cargo, features, editions