我希望迭代器被过滤,但是我的谓词有可能失败。当谓词失败时,我希望整个函数也失败。在这个例子中,我希望work
返回由maybe
生成的Result
:
fn maybe(v: u32) -> Result<bool, u8> {
match v % 3 {
0 => Ok(true),
1 => Ok(false),
2 => Err(42),
}
}
fn work() -> Result<Vec<u32>, u8> {
[1, 2, 3, 4, 5].iter().filter(|&&x| maybe(x)).collect()
}
fn main() {
println!("{:?}", work())
}
error[E0308]: mismatched types
--> src/main.rs:10:45
|
10 | [1, 2, 3, 4, 5].iter().filter(|&&x| maybe(x)).collect()
| ^^^^^^^^ expected bool, found enum `std::result::Result`
|
= note: expected type `bool`
found type `std::result::Result<bool, u8>`
error[E0277]: the trait bound `std::result::Result<std::vec::Vec<u32>, u8>: std::iter::FromIterator<&u32>` is not satisfied
--> src/main.rs:10:55
|
10 | [1, 2, 3, 4, 5].iter().filter(|&&x| maybe(x)).collect()
| ^^^^^^^ a collection of type `std::result::Result<std::vec::Vec<u32>, u8>` cannot be built from an iterator over elements of type `&u32`
|
= help: the trait `std::iter::FromIterator<&u32>` is not implemented for `std::result::Result<std::vec::Vec<u32>, u8>`
try!(maybe(x))
并将值推入一个可变的Vec<u32>
。 - rodrigo