Skip to main content
Module 7 · Lesson 2 of 214/24 in the course~20 min
Module lessons (2/2)

Result<T, E>, Operator ? and Custom Error

Lesson preview5 hands-on exercises readyCourse Rust · Module 7 — Collections and Error Handling

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 match by propagating errors with the idiomatic operator ?.
  • Create domain error types (enum AppError) by converting underlying errors using the From trait.

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

  1. 11. Anatomy of Result<T, E> and the ? operator
  2. 22. Pipeline Interactive Simulator Result<T, E> and ?
  3. 33. Custom Error with enum and From
  4. 44. When to use unwrap() vs expect() vs ?
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 7 — Collections and Error Handling

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.