|
45 | 45 | //! that make working with it more succinct.
|
46 | 46 | //!
|
47 | 47 | //! ```
|
| 48 | +//! // The `is_ok` and `is_err` methods do what they say. |
48 | 49 | //! let good_result: Result<i32, i32> = Ok(10);
|
49 | 50 | //! let bad_result: Result<i32, i32> = Err(10);
|
50 |
| -//! |
51 |
| -//! // The `is_ok` and `is_err` methods do what they say. |
52 | 51 | //! assert!(good_result.is_ok() && !good_result.is_err());
|
53 | 52 | //! assert!(bad_result.is_err() && !bad_result.is_ok());
|
54 | 53 | //!
|
55 |
| -//! // `map` consumes the `Result` and produces another. |
| 54 | +//! // `map` and `map_err` consume the `Result` and produces another. |
56 | 55 | //! let good_result: Result<i32, i32> = good_result.map(|i| i + 1);
|
57 |
| -//! let bad_result: Result<i32, i32> = bad_result.map(|i| i - 1); |
| 56 | +//! let bad_result: Result<i32, i32> = bad_result.map_err(|i| i - 1); |
| 57 | +//! assert_eq!(good_result, Ok(11)); |
| 58 | +//! assert_eq!(bad_result, Err(9)); |
58 | 59 | //!
|
59 | 60 | //! // Use `and_then` to continue the computation.
|
60 | 61 | //! let good_result: Result<bool, i32> = good_result.and_then(|i| Ok(i == 11));
|
| 62 | +//! assert_eq!(good_result, Ok(true)); |
61 | 63 | //!
|
62 | 64 | //! // Use `or_else` to handle the error.
|
63 | 65 | //! let bad_result: Result<i32, i32> = bad_result.or_else(|i| Ok(i + 20));
|
| 66 | +//! assert_eq!(bad_result, Ok(29)); |
64 | 67 | //!
|
65 | 68 | //! // Consume the result and return the contents with `unwrap`.
|
66 | 69 | //! let final_awesome_result = good_result.unwrap();
|
| 70 | +//! assert_eq!(final_awesome_result) |
67 | 71 | //! ```
|
68 | 72 | //!
|
69 | 73 | //! # Results must be used
|
|
0 commit comments