I need to chunk my vector Vec<Result<SomeStruct>>
into pieces, iterate over each piece, do some work, and return an error if Result<SomeStruct>
contains it.
enum SomeStruct {}
#[derive(Debug)]
struct SomeError {}
type Result<T> = std::result::Result<T, SomeError>;
fn foo(v: Vec<Result<SomeStruct>>) -> Result<()> {
for chunk in v.chunks(42) {
for value in chunk {
let value = value?;
//do smth with value
}
}
Ok(())
}
fn main() {
let mut values = Vec::new();
foo(values).unwrap();
}
However, I get the error
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
--> src/main.rs:11:25
|
11 | let value = value?;
| ^^^^^^ the `?` operator cannot be applied to type `&std::result::Result<SomeStruct, SomeError>`
|
= help: the trait `std::ops::Try` is not implemented for `&std::result::Result<SomeStruct, SomeError>`
= note: required by `std::ops::Try::into_result`
It seems ?
expects the value itself instead of a shared reference to the value, but I can't dereference a shared reference and don't know how to get this value itself because chunk
here is &[Result<SomeStruct>]
, and value
is &Result<SomeStruct>
How can I fix it?