Skip to content

Commit

Permalink
Rollup merge of #105034 - HintringerFabian:improve_iterator_flatten_d…
Browse files Browse the repository at this point in the history
…oc, r=cuviper

Add example for iterator_flatten

Adds an Example to iterator_flatten
Fixes #82687
  • Loading branch information
Yuki Okushi authored Jan 9, 2023
2 parents 3020239 + c364d32 commit 002eccc
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,18 @@ pub trait Iterator {
/// assert_eq!(merged, "alphabetagamma");
/// ```
///
/// Flattening works on any `IntoIterator` type, including `Option` and `Result`:
///
/// ```
/// let options = vec![Some(123), Some(321), None, Some(231)];
/// let flattened_options: Vec<_> = options.into_iter().flatten().collect();
/// assert_eq!(flattened_options, vec![123, 321, 231]);
///
/// let results = vec![Ok(123), Ok(321), Err(456), Ok(231)];
/// let flattened_results: Vec<_> = results.into_iter().flatten().collect();
/// assert_eq!(flattened_results, vec![123, 321, 231]);
/// ```
///
/// Flattening only removes one level of nesting at a time:
///
/// ```
Expand Down

0 comments on commit 002eccc

Please sign in to comment.