Add example for iterator_flatten
This commit is contained in:
parent
8a09420ac4
commit
76438d26b1
1 changed files with 8 additions and 0 deletions
|
|
@ -1495,6 +1495,14 @@ pub trait Iterator {
|
|||
/// assert_eq!(merged, "alphabetagamma");
|
||||
/// ```
|
||||
///
|
||||
/// Flattening also works on other types like Option and Result:
|
||||
///
|
||||
/// ```
|
||||
/// let values = vec![Some(123), Some(321), None, Some(231)];
|
||||
/// let flattened_values: Vec<_> = values.into_iter().flatten().collect();
|
||||
/// assert_eq!(flattened_values, vec![123, 321, 231]);
|
||||
/// ```
|
||||
///
|
||||
/// You can also rewrite this in terms of [`flat_map()`], which is preferable
|
||||
/// in this case since it conveys intent more clearly:
|
||||
///
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue