Module lessons (2/2)
Result<T, E>, Operator ? and Custom Error
Distinguish panic! from Result, propagate errors with the ? operator, implement From, and expose typed enum AppError.
What you'll learn in this lesson
- Separate unrecoverable errors (
panic!) from manageable errors (Result<T, E>). - Eliminate nested
matchby propagating errors with the idiomatic operator?. - Create domain error types (
enum AppError) by converting underlying errors using theFromtrait.
Overview
Rust does not have try / catch or exceptions that silently move up the stack. It strictly distinguishes two categories: internal bug errors (panic!) and externally expected errors such as missing files or malformed input (Result<T, E>).
Result<T, E> has two variants: Ok(T) and Err(E). The ? operator applied to a Result extracts the value from Ok(v), while in the case of Err(e) it performs an automatic early return by converting e via From::from(e):
Sections covered in this lesson
- 11. Anatomy of
Result<T, E>and the?operator - 22. Pipeline Interactive Simulator
Result<T, E>and? - 33. Custom Error with
enumandFrom - 44. When to use
unwrap()vsexpect()vs? - 5Practical exercises
- 6Check for understanding
Unlock "Result<T, E>, Operator ? and Custom Error" and the full Course Rust curriculum
Distinguish `panic!` from `Result`, propagate errors with the `?` operator, implement `From`, and expose typed `enum AppError`.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Rust plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.