std-method
Iterator::flatten — remove one nesting layer
Turn a sequence of iterable items into one sequence. An Option yields zero or one value; flatten can therefore omit absent values. It removes one layer, not arbitrary nesting. Expected stdout (not executed): [18, 21] Common mistake: Using flatten on Result values without realizing errors are omitted.
Formal Syntax & Structure
rust syntaxrust
fn flatten(self) -> Flatten<Self> where Self: Sized, Self::Item: IntoIteratorWorking Example
Example Snippetrust
fn main() { let readings = [Some(18), None, Some(21)]; let valid: Vec<_> = readings.into_iter().flatten().collect(); println!("{valid:?}");}