Back to library

πŸ¦€Master Rust Error Handling with Result, ? and thiserror

Stop reaching for unwrap() and panic!. Learn Rust's error-as-value model, then wield Result, ?, anyhow, and thiserror well enough to design a clean error hierarchy for a small CLI.

Applied14 drops~2-week path Β· 5–8 min/daytechnology

Phase 1Errors as Values, Not Exceptions

See errors as values, not runtime exceptions.

4 drops
  1. Rust has no exceptions β€” and that is the point

    6 min

    Rust has no exceptions β€” and that is the point

  2. Option is a missing value β€” Result is a failed operation

    5 min

    Option is a missing value β€” Result is a failed operation

  3. panic! is for impossible β€” not for inconvenient

    6 min

    panic! is for impossible β€” not for inconvenient

  4. Pattern-match until the ? operator earns its place

    6 min

    Pattern-match until the ? operator earns its place

Phase 2Propagating Errors with ? and Conversions

Convert unwrap and panic into Result with ?.

5 drops
  1. ? is early-return plus a type coercion

    6 min

    ? is early-return plus a type coercion

  2. Turn one error into another without losing the trail

    6 min

    Turn one error into another without losing the trail

  3. map, and_then, or_else β€” Result as a pipeline

    6 min

    map, and_then, or_else β€” Result as a pipeline

  4. Your main function can return Result β€” and probably should

    5 min

    Your main function can return Result β€” and probably should

  5. Refactor one unwrap at a time β€” not all at once

    6 min

    Refactor one unwrap at a time β€” not all at once

Phase 3Choosing anyhow, thiserror, or Custom Enums

Choose between anyhow, thiserror, and custom enums.

4 drops
  1. A scenario β€” anyhow or thiserror?

    7 min

    A scenario β€” anyhow or thiserror?

  2. Your CLI crashed at 3am β€” what does the log tell you?

    7 min

    Your CLI crashed at 3am β€” what does the log tell you?

  3. You're publishing a crate β€” design its errors like an API

    7 min

    You're publishing a crate β€” design its errors like an API

  4. Your binary imports your own library β€” where do anyhow and thiserror meet?

    7 min

    Your binary imports your own library β€” where do anyhow and thiserror meet?

Phase 4Designing a CLI Error Hierarchy

Design an error hierarchy for a real CLI.

1 drop
  1. Design and ship an error hierarchy for a small CLI

    8 min

    Design and ship an error hierarchy for a small CLI

Frequently asked questions

Why doesn't Rust use try/catch like Java or Python?
This is covered in the β€œMaster Rust Error Handling with Result, ? and thiserror” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
When should I use anyhow versus thiserror?
This is covered in the β€œMaster Rust Error Handling with Result, ? and thiserror” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What does the ? operator actually do under the hood?
This is covered in the β€œMaster Rust Error Handling with Result, ? and thiserror” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
How do I convert between different error types cleanly?
This is covered in the β€œMaster Rust Error Handling with Result, ? and thiserror” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Is it ever okay to use unwrap() in production code?
This is covered in the β€œMaster Rust Error Handling with Result, ? and thiserror” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.