use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let numbers = vec!["8", "9", "1", "3"];
// works as expected
let integer = numbers[0].parse::<i64>()?;
dbg!(integer);
// i want it to collect the parsed numbers or return with the first error in the main function
let integers = numbers.iter().map(|number| number.parse::<i64>()?).collect();
Ok(())
}
Basically, I want to map a failable function on the elements of an iterator which is to be consumed, and want it to either return the full result or return the first error
That's exactly what impl FromIterator for Result does:
fn main() -> Result<(), Box<dyn Error>> {
let numbers = vec!["8", "9", "1", "3"];
// We just move the question mark to the very end of pipeline, after collecting
let integers = numbers
.iter()
.map(|number| number.parse::<i64>())
// And we explicitly ask to collect into `Result<Vec>`
.collect::<Result<Vec<_>, _>>()?;
println!("{:?}", integers);
Ok(())
}