Skip to content

Commit adfac00

Browse files
committed
Result::and_then: show type conversion
1 parent 7eaecc6 commit adfac00

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

library/core/src/result.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1282,14 +1282,13 @@ impl<T, E> Result<T, E> {
12821282
/// # Examples
12831283
///
12841284
/// ```
1285-
/// fn sq(x: u32) -> Result<u32, &'static str> {
1286-
/// x.checked_mul(x).ok_or("overflowed")
1285+
/// fn sq_then_to_string(x: u32) -> Result<String, &'static str> {
1286+
/// x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed")
12871287
/// }
12881288
///
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-
1289+
/// assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string()));
1290+
/// assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed"));
1291+
/// assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number"));
12931292
/// ```
12941293
///
12951294
/// Often used to chain fallible operations that may return [`Err`].

0 commit comments

Comments
 (0)