about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2022-02-12 12:12:11 +0800
committercyqsimon <28627918+cyqsimon@users.noreply.github.com>2022-02-12 12:12:11 +0800
commit7eaecc6508149da3a0e76982f1259f5bf3cf54a8 (patch)
treeadaa41f5d7666b68df702882b92206c9de6b2c63
parent942eaa7ffcebad5592bf6865ef8c9e8959ec5b79 (diff)
downloadrust-7eaecc6508149da3a0e76982f1259f5bf3cf54a8.tar.gz
rust-7eaecc6508149da3a0e76982f1259f5bf3cf54a8.zip
`Result::and_then`: improve basic example
-rw-r--r--library/core/src/result.rs10
1 files changed, 6 insertions, 4 deletions
diff --git a/library/core/src/result.rs b/library/core/src/result.rs
index 58d58ff0f72..d6a0c7dc292 100644
--- a/library/core/src/result.rs
+++ b/library/core/src/result.rs
@@ -1282,12 +1282,14 @@ impl<T, E> Result<T, E> {
     /// # Examples
     ///
     /// ```
-    /// fn squared_string(x: u32) -> Result<String, &'static str> {
-    ///     Ok((x * x).to_string())
+    /// fn sq(x: u32) -> Result<u32, &'static str> {
+    ///     x.checked_mul(x).ok_or("overflowed")
     /// }
     ///
-    /// assert_eq!(Ok(2).and_then(squared_string), Ok(4.to_string()));
-    /// assert_eq!(Err("not a number").and_then(squared_string), Err("not a number"));
+    /// 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"));
+
     /// ```
     ///
     /// Often used to chain fallible operations that may return [`Err`].