error-concept
Collect an iterator of optional values
Convert a sequence of optional items into one optional vector. Any encountered None causes an early None; otherwise the result contains every value, including an empty vector for empty input. Expected stdout (not executed): None Some([]) Common mistake: Expecting missing entries to be skipped, as with flatten.
Formal Syntax & Structure
rust syntaxrust
Iterator<Item = Option<T>>::collect::<Option<Vec<T>>>()Working Example
Example Snippetrust
fn main() { let readings = [Some(5), None, Some(8)]; let batch: Option<Vec<_>> = readings.into_iter().collect(); let empty: Option<Vec<u32>> = std::iter::empty::<Option<u32>>().collect(); println!("{batch:?} {empty:?}");}