If you’ve made it this far, you’re probably overwhelmed by all the different methods and their uses! It helps to try and learn to use them one at a time; as and when needed. The following table summarises which method you would use under different circumstances.

What do you want to do? Method to use
Create a Result
  • Constructor: Ok(value)
  • Constructor: Err(value)
Get both values out of a Result
  • Patten matching
  • map_or_else
  • map_or
  • unwrap panics
  • unwrap_or
  • unwrap_or_else
  • unwrap_or_default
  • expect panics
Run a function on the value inside Ok
  • map
  • and_then
Run a function on the value inside Err
  • or_else
  • map_err
Get the value inside Ok
  • unwrap panics
  • expect panics
  • ? operator
Get the value inside Ok with fallback
  • Patten matching
  • map_or
  • unwrap_or
  • unwrap_or_else
  • unwrap_or_default
Get the value inside Err
  • unwrap_err panics
  • expect_err panics
Get the value inside Err with fallback
  • Patten matching
  • map_or
  • unwrap_or
  • unwrap_or_else
  • unwrap_or_default
Combine two Results that are Ok
  • and_then
  • and
  • ? operator
Combine two Results that are Err
  • or
  • or_else
Convert Ok to Option as Some
  • ok
Convert Err to Option as Some
  • err
Convert Result<Option> to Option<Result>
  • transpose
Test for Ok
  • is_ok
Test for Err
  • is_err
Test for Ok and run a predicate
  • is_ok_and
Test for Err and run a predicate
  • is_err_and

I hope this somewhat lengthy series helped you learn some more about Rust’s Result type. If you found any errors or omissions or found this useful, please leave a comment.

Feedback from the review lounge

SirKastic23

A huge thanks to SirKastic23 for giving me some sound feedback on Reddit:

Comment
byu/ssanjs from discussion
inrust

I’ve made the updates to the sections marked “unsafe”. I’ll get to dark mode in the near future.