Skip to content

Commit 7eaecc6

Browse files
committed
Result::and_then: improve basic example
1 parent 942eaa7 commit 7eaecc6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

library/core/src/result.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,12 +1282,14 @@ impl<T, E> Result<T, E> {
12821282
/// # Examples
12831283
///
12841284
/// ```
1285-
/// fn squared_string(x: u32) -> Result<String, &'static str> {
1286-
/// Ok((x * x).to_string())
1285+
/// fn sq(x: u32) -> Result<u32, &'static str> {
1286+
/// x.checked_mul(x).ok_or("overflowed")
12871287
/// }
12881288
///
1289-
/// assert_eq!(Ok(2).and_then(squared_string), Ok(4.to_string()));
1290-
/// assert_eq!(Err("not a number").and_then(squared_string), Err("not a number"));
1289+
/// assert_eq!(Ok(2).and_then(sq), Ok(4));
1290+
/// assert_eq!(Ok(1_000_000).and_then(sq), Err("overflowed"));
1291+
/// assert_eq!(Err("not a number").and_then(sq), Err("not a number"));
1292+
12911293
/// ```
12921294
///
12931295
/// Often used to chain fallible operations that may return [`Err`].

0 commit comments

Comments
 (0)