error-concept
Collect an iterator of Result values
Collect successful values into a vector, or return the first error encountered. Using by_ref keeps the original iterator available at its next item. Already collected values are not returned on error. Expected stdout (not executed): Err("bad row") Some(Ok(9)) Common mistake: Collecting Vec<Result<T,E>> when the desired contract is all-or-error.
Formal Syntax & Structure
rust syntaxrust
Iterator<Item = Result<T, E>>::collect::<Result<Vec<T>, E>>()Working Example
Example Snippetrust
fn main() { let mut rows = [Ok(12), Err("bad row"), Ok(9)].into_iter(); let batch: Result<Vec<_>, _> = rows.by_ref().collect(); println!("{batch:?} {:?}", rows.next());}