From adfac00f458c7b68efb759335a804949d2f64d8b Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sat, 12 Feb 2022 12:19:03 +0800 Subject: [PATCH] `Result::and_then`: show type conversion --- library/core/src/result.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index d6a0c7dc2929..334a89907870 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1282,14 +1282,13 @@ impl Result { /// # Examples /// /// ``` - /// fn sq(x: u32) -> Result { - /// x.checked_mul(x).ok_or("overflowed") + /// fn sq_then_to_string(x: u32) -> Result { + /// x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") /// } /// - /// assert_eq!(Ok(2).and_then(sq), Ok(4)); - /// assert_eq!(Ok(1_000_000).and_then(sq), Err("overflowed")); - /// assert_eq!(Err("not a number").and_then(sq), Err("not a number")); - + /// assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string())); + /// assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed")); + /// assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number")); /// ``` /// /// Often used to chain fallible operations that may return [`Err`].