Working With Rust Result
Trying to learning how to use the Rust Result type can be daunting. In this “Working with Rust Result” series of short posts, I hope to make that more approachable. This series is for beginners who are finding it difficult to understand what a Result
is and how to use it.
The series is split into fourteen parts as listed below.
- What is a Result? - (Construction)
- Extracting Values - (Pattern matching, map_or_else, map_or)
- Extracting Values That Can Panic - (unwrap, expect)
- Making Things Nicer with Fallbacks - (unwrap_or, unwrap_or_else, unwrap_or_default)
- Transforming Values - (map)
- Combining Results - (and_then, Aligning error types)
- Chaining with Map
- Combining Results the Question Mark Operator - (The question mark operator, Keep aligning those error values)
- Combining Results Some More - (and, or, or_else)
- Working with Errors - (map_err, unwrap_err, expect_err)
- Conversion to Option - (ok, err, transpose)
- Value Tests - (is_ok, is_err, is_ok_and, is_err_and)
- Asides - (Functions that return Result in std, Eager vs Laziness)
- Summary - (Cheatsheet, Feedback from the review lounge)
Now I know what you’re thinking:
Fourteen posts? You’ve got to be kidding me!
I know it’s a lot of posts. I’ve tried to make each as small as possible with a single focus. I’ve added examples and some diagrams to make it more palatable.
Also don’t feel the need to read the full series at one go. Read as much as you want or choose a topic you want to know more about or are currently struggling with and start there. Be sure to try some of the examples out and experiment with your own changes; That’s the best way to learn.
Jump in at What is a Result?