diff options
| author | bors <bors@rust-lang.org> | 2024-05-16 12:21:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-05-16 12:21:12 +0000 |
| commit | 4a78c00e227124ff9d5ece2d493472e7325c87d3 (patch) | |
| tree | b9a6e6299f2b14da5f3ab141ea86cb1eb1e9e4d8 | |
| parent | bf8801d36dfd28de7d3b0279b53d38593acdfd14 (diff) | |
| parent | e1611aa690ccfa2e38b0041df90bca424809226d (diff) | |
| download | rust-4a78c00e227124ff9d5ece2d493472e7325c87d3.tar.gz rust-4a78c00e227124ff9d5ece2d493472e7325c87d3.zip | |
Auto merge of #124959 - prorealize:update-result-documentation, r=joboet
Refactor examples and enhance documentation in result.rs - Replaced `map` with `map_err` in the error handling example for correctness - Reordered example code to improve readability and logical flow - Added assertions to examples to demonstrate expected outcomes
| -rw-r--r-- | library/core/src/result.rs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/library/core/src/result.rs b/library/core/src/result.rs index b2b627fe6a9..4c6dc4bba43 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -45,25 +45,29 @@ //! that make working with it more succinct. //! //! ``` +//! // The `is_ok` and `is_err` methods do what they say. //! let good_result: Result<i32, i32> = Ok(10); //! let bad_result: Result<i32, i32> = Err(10); -//! -//! // The `is_ok` and `is_err` methods do what they say. //! assert!(good_result.is_ok() && !good_result.is_err()); //! assert!(bad_result.is_err() && !bad_result.is_ok()); //! -//! // `map` consumes the `Result` and produces another. +//! // `map` and `map_err` consume the `Result` and produce another. //! let good_result: Result<i32, i32> = good_result.map(|i| i + 1); -//! let bad_result: Result<i32, i32> = bad_result.map(|i| i - 1); +//! let bad_result: Result<i32, i32> = bad_result.map_err(|i| i - 1); +//! assert_eq!(good_result, Ok(11)); +//! assert_eq!(bad_result, Err(9)); //! //! // Use `and_then` to continue the computation. //! let good_result: Result<bool, i32> = good_result.and_then(|i| Ok(i == 11)); +//! assert_eq!(good_result, Ok(true)); //! //! // Use `or_else` to handle the error. //! let bad_result: Result<i32, i32> = bad_result.or_else(|i| Ok(i + 20)); +//! assert_eq!(bad_result, Ok(29)); //! //! // Consume the result and return the contents with `unwrap`. //! let final_awesome_result = good_result.unwrap(); +//! assert!(final_awesome_result) //! ``` //! //! # Results must be used |
