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.

Cheatsheet

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
Run a function on the value inside Ok
Run a function on the value inside Err
Get the value inside Ok
Get the value inside Ok with fallback
Get the value inside Err
Get the value inside Err with fallback
Combine two Results that are Ok
Combine two Results that are Err
Convert Ok to Option as Some
Convert Err to Option as Some
Convert Result<Option> to Option<Result>
Test for Ok
Test for Err
Test for Ok and run a predicate
Test for Err and run a predicate

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’ve also made the blog “Dark Mode” by default.

tobikris

Another huge thanks to tobikris for pointing out an error with my usage of eager functions:

Comment
byu/ssanjs from discussion
inrust

I’ve added clearer warnings regarding eagerness and have updated the affected examples.

someboddy

Thanks to someboddy for suggesting linking to the individual sections. I’ve added a link to every method covered, in the TOC and in the summary page.

Comment
byu/ssanjs from discussion
inrust