Skip to content

Commit 4a78c00

Browse files
committed
Auto merge of rust-lang#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
2 parents bf8801d + e1611aa commit 4a78c00

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

library/core/src/result.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,29 @@
4545
//! that make working with it more succinct.
4646
//!
4747
//! ```
48+
//! // The `is_ok` and `is_err` methods do what they say.
4849
//! let good_result: Result<i32, i32> = Ok(10);
4950
//! let bad_result: Result<i32, i32> = Err(10);
50-
//!
51-
//! // The `is_ok` and `is_err` methods do what they say.
5251
//! assert!(good_result.is_ok() && !good_result.is_err());
5352
//! assert!(bad_result.is_err() && !bad_result.is_ok());
5453
//!
55-
//! // `map` consumes the `Result` and produces another.
54+
//! // `map` and `map_err` consume the `Result` and produce another.
5655
//! 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));
5859
//!
5960
//! // Use `and_then` to continue the computation.
6061
//! let good_result: Result<bool, i32> = good_result.and_then(|i| Ok(i == 11));
62+
//! assert_eq!(good_result, Ok(true));
6163
//!
6264
//! // Use `or_else` to handle the error.
6365
//! let bad_result: Result<i32, i32> = bad_result.or_else(|i| Ok(i + 20));
66+
//! assert_eq!(bad_result, Ok(29));
6467
//!
6568
//! // Consume the result and return the contents with `unwrap`.
6669
//! let final_awesome_result = good_result.unwrap();
70+
//! assert!(final_awesome_result)
6771
//! ```
6872
//!
6973
//! # Results must be used

0 commit comments

Comments
 (0)