From 160faf1b30d22b6a6958267e6f7bc3fb2b03754f Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sat, 12 Feb 2022 12:23:38 +0800 Subject: [PATCH] `Option::and_then` basic example: show failure --- library/core/src/option.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 9fe38a505ab..508837f63c3 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1207,10 +1207,13 @@ impl Option { /// # Examples /// /// ``` - /// fn squared_string(x: u32) -> Option { Some((x * x).to_string()) } + /// fn sq_then_to_string(x: u32) -> Option { + /// x.checked_mul(x).map(|sq| sq.to_string()) + /// } /// - /// assert_eq!(Some(2).and_then(squared_string), Some(4.to_string())); - /// assert_eq!(None.and_then(squared_string), None); + /// assert_eq!(Some(2).and_then(sq_then_to_string), Some(4.to_string())); + /// assert_eq!(Some(1_000_000).and_then(sq_then_to_string), None); // overflowed! + /// assert_eq!(None.and_then(sq_then_to_string), None); /// ``` /// /// Often used to chain fallible operations that may return [`None`].