Skip to content

Commit 4adf99b

Browse files
Rollup merge of rust-lang#105034 - HintringerFabian:improve_iterator_flatten_doc, r=cuviper
Add example for iterator_flatten Adds an Example to iterator_flatten Fixes rust-lang#82687
2 parents 3020239 + c364d32 commit 4adf99b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,18 @@ pub trait Iterator {
15141514
/// assert_eq!(merged, "alphabetagamma");
15151515
/// ```
15161516
///
1517+
/// Flattening works on any `IntoIterator` type, including `Option` and `Result`:
1518+
///
1519+
/// ```
1520+
/// let options = vec![Some(123), Some(321), None, Some(231)];
1521+
/// let flattened_options: Vec<_> = options.into_iter().flatten().collect();
1522+
/// assert_eq!(flattened_options, vec![123, 321, 231]);
1523+
///
1524+
/// let results = vec![Ok(123), Ok(321), Err(456), Ok(231)];
1525+
/// let flattened_results: Vec<_> = results.into_iter().flatten().collect();
1526+
/// assert_eq!(flattened_results, vec![123, 321, 231]);
1527+
/// ```
1528+
///
15171529
/// Flattening only removes one level of nesting at a time:
15181530
///
15191531
/// ```

0 commit comments

Comments
 (0)