about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2022-02-12 12:23:38 +0800
committercyqsimon <28627918+cyqsimon@users.noreply.github.com>2022-02-12 12:23:38 +0800
commit160faf1b30d22b6a6958267e6f7bc3fb2b03754f (patch)
treeeb5a1a707603177c29710f23ec6cb29e274d92c9
parentadfac00f458c7b68efb759335a804949d2f64d8b (diff)
downloadrust-160faf1b30d22b6a6958267e6f7bc3fb2b03754f.tar.gz
rust-160faf1b30d22b6a6958267e6f7bc3fb2b03754f.zip
`Option::and_then` basic example: show failure
-rw-r--r--library/core/src/option.rs9
1 files 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<T> Option<T> {
     /// # Examples
     ///
     /// ```
-    /// fn squared_string(x: u32) -> Option<String> { Some((x * x).to_string()) }
+    /// fn sq_then_to_string(x: u32) -> Option<String> {
+    ///     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`].