Skip to content

Commit 160faf1

Browse files
committed
Option::and_then basic example: show failure
1 parent adfac00 commit 160faf1

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

library/core/src/option.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1207,10 +1207,13 @@ impl<T> Option<T> {
12071207
/// # Examples
12081208
///
12091209
/// ```
1210-
/// fn squared_string(x: u32) -> Option<String> { Some((x * x).to_string()) }
1210+
/// fn sq_then_to_string(x: u32) -> Option<String> {
1211+
/// x.checked_mul(x).map(|sq| sq.to_string())
1212+
/// }
12111213
///
1212-
/// assert_eq!(Some(2).and_then(squared_string), Some(4.to_string()));
1213-
/// assert_eq!(None.and_then(squared_string), None);
1214+
/// assert_eq!(Some(2).and_then(sq_then_to_string), Some(4.to_string()));
1215+
/// assert_eq!(Some(1_000_000).and_then(sq_then_to_string), None); // overflowed!
1216+
/// assert_eq!(None.and_then(sq_then_to_string), None);
12141217
/// ```
12151218
///
12161219
/// Often used to chain fallible operations that may return [`None`].

0 commit comments

Comments
 (0)