Let's go through some steps to handle errors seamlessly:
- Create a new project with cargo new exceptional-results --lib and open it with VS Code.
- Open src/lib.rs and replace the existing test with a new test:
#[test] fn positive_results() { // code goes here }
- As the name suggests, we'll add some positive result tests in the function's body. Let's start with a declaration and something simple. Replace the preceding // code goes here section with the following:
let ok: Result<i32, f32> = Ok(42); assert_eq!(ok.and_then(|r| Ok(r + 1)), Ok(43)); assert_eq!(ok.map(|r| r + 1), Ok(43));
- Let's add some more variation since multiple Result types can behave just like Booleans. Add some more code into the good_results test: