Whenever a binary result is to be returned from a function, the choice is between using Result or Option. Both can communicate a failed function call—but the former provides too much specificity, while the latter may give too little. While this is a decision to make for the specific situation, Rust's types provide the tools to move between them with ease. Let's go over them in this recipe.
Moving between Option and Result
How to do it...
In a few quick steps, you'll know how to move between Option and Result:
- Create a new project with cargo new options-results --lib and open it with VS Code.
- Let's edit src/lib.rs and replace the existing test (inside mod tests) with the following:
#[derive...