std-method
Iterator::flat_map — expand each item
Map each input to an iterable and concatenate its yielded items. Useful when each order contains multiple products. Expected stdout (not executed): [10, 11, 20, 21, 30, 31] Common mistake: Using map when a single flattened sequence is required.
Formal Syntax & Structure
rust syntaxrust
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where U: IntoIterator, F: FnMut(Self::Item) -> U, Self: SizedWorking Example
Example Snippetrust
fn main() { let ids: Vec<_> = (1..=3).flat_map(|n| [n * 10, n * 10 + 1]).collect(); println!("{ids:?}");}