Rust error handling: Result, Option and the ? operator
10 steps · about 3 h 35 min · Intermediate
- Error Handling - The Rust Programming Language (doc, doc.rust-lang.org (The Rust Book), 5 min)
It starts with the big idea that Rust has no exceptions, only Result for recoverable errors and panic! for unrecoverable ones.
- Recoverable Errors with Result - The Rust Programming Language (doc, doc.rust-lang.org (The Rust Book), 30 min)
The official explanation of matching on Result, unwrap, propagating errors, and the ? operator with its From conversion.
- Rust Result (tutorial, rusttutorial.org, 20 min)
A short second explanation of Ok, Err and ?, followed by a practice exercise that has you add two parsed numbers with ?.
- Option and Result · Learn Rust (interactive, learningrust.org, 30 min)
It covers Option and Result combinators and shows the compiler error you get when you use ? on an Option inside a Result function.
- Rust error handling - Result, the ? operator, and custom errors (tutorial, zetcode.com, 20 min)
Runnable examples of ? with Option, ok and ok_or conversions, and returning Result from main.
- Result - Rust By Example (doc, doc.rust-lang.org (Rust By Example), 15 min)
Short, editable examples that show Result as a richer Option and how parse failures flow through main.
- To panic! or Not to panic! - The Rust Programming Language (doc, doc.rust-lang.org (The Rust Book), 15 min)
Once you can use Result, this chapter teaches you when to return it and when a panic is acceptable.
- Rust Error Handling with anyhow and thiserror (tutorial, rusttutorial.org, 25 min)
It introduces the two main error crates: thiserror for defining error types and anyhow for passing errors up with context.
- Error Handling in Rust: Result, ?, anyhow, and thiserror (article, blog.teliaz.com, 15 min)
A decision guide that ties ?, From conversions, typed library errors and application context together.
- Error Handling In Rust - A Deep Dive (article, Luca Palmieri (lpalmieri.com), 40 min)
It ends the list by questioning the simple rule that thiserror is for libraries and anyhow is for applications, with a framework for deciding who needs to see an error.