I have a function which returns a Result<u32, SomeError>. I want to collect four values from this function, and collect them to sum them up. How can you propagate the error inside the map operator? I want to stop on the first error, and propagate the error up to the function which called it
Works
(0..4)
.map(|_| self.consume().unwrap())
.collect::<Vec<u32>>()
.iter()
.sum::<u32>();
Does not work
(0..4)
.map(|_| self.consume()?)
.collect::<Vec<u32>>()
.iter()
.sum::<u32>();
The compiler error is
.map(|_| self.consume()?)
----^^^^^^^^^^^^^^^----------------------
| |
| cannot use the `?` operator in a closure that returns `u32`
this function should return `Result` or `Option` to accept `?`