We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Option::and_then
1 parent adfac00 commit 160faf1Copy full SHA for 160faf1
library/core/src/option.rs
@@ -1207,10 +1207,13 @@ impl<T> Option<T> {
1207
/// # Examples
1208
///
1209
/// ```
1210
- /// fn squared_string(x: u32) -> Option<String> { Some((x * x).to_string()) }
+ /// fn sq_then_to_string(x: u32) -> Option<String> {
1211
+ /// x.checked_mul(x).map(|sq| sq.to_string())
1212
+ /// }
1213
- /// assert_eq!(Some(2).and_then(squared_string), Some(4.to_string()));
- /// 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);
1217
1218
1219
/// Often used to chain fallible operations that may return [`None`].
0 commit comments