【发布时间】:2023-04-09 14:43:01
【问题描述】:
我想使用一个可能返回Err 结果的过滤器函数,并将其冒泡到包含函数:
mycoll.into_iter()
.filter(|el| {
if el == "bad" {
Err(MyError)
} else {
Ok(el < "foo")
}
})
当涉及到map()(使用.collect::<Result<...>>())时,我找到了一个很好的解释:How do I stop iteration and return an error when Iterator::map returns a Result::Err?,但我无法为filter()找到类似的解决方案。
这里的惯用解决方案是什么?
【问题讨论】:
标签: filter error-handling rust iterator