【发布时间】:2019-06-28 14:17:23
【问题描述】:
在编写这样的代码时,我想要某种方式来进行这样的转换:
struct Value;
fn remove_missed(uncertain_vector: Vec<Option<Value>>) -> Vec<Value> {
uncertain_vector
.into_iter()
.filter(|element| match element {
Some(val) => true,
None => false,
})
.collect()
}
我怎样才能做到这一点?我认为类型暗示机制不够聪明,无法确定生成的集合将仅包含 Option<Value>,其中所有此类对象的类型都相同 (Value)。
编译器部分回答了我的问题:
error[E0277]: a collection of type `std::vec::Vec<Value>` cannot be built from an iterator over elements of type `std::option::Option<Value>`
--> src/lib.rs:10:10
|
10 | .collect()
| ^^^^^^^ a collection of type `std::vec::Vec<Value>` cannot be built from `std::iter::Iterator<Item=std::option::Option<Value>>`
|
= help: the trait `std::iter::FromIterator<std::option::Option<Value>>` is not implemented for `std::vec::Vec<Value>`
【问题讨论】:
标签: functional-programming rust iterator