Skip to content

Commit 76438d2

Browse files
Add example for iterator_flatten
1 parent 8a09420 commit 76438d2

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

library/core/src/iter/traits/iterator.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,14 @@ pub trait Iterator {
14951495
/// assert_eq!(merged, "alphabetagamma");
14961496
/// ```
14971497
///
1498+
/// Flattening also works on other types like Option and Result:
1499+
///
1500+
/// ```
1501+
/// let values = vec![Some(123), Some(321), None, Some(231)];
1502+
/// let flattened_values: Vec<_> = values.into_iter().flatten().collect();
1503+
/// assert_eq!(flattened_values, vec![123, 321, 231]);
1504+
/// ```
1505+
///
14981506
/// You can also rewrite this in terms of [`flat_map()`], which is preferable
14991507
/// in this case since it conveys intent more clearly:
15001508
///

0 commit comments

Comments
 (0)