相关疑难解决方法(0)

我如何收集到阵列?

我想调用.map()一系列枚举:

enum Foo {
    Value(i32),
    Nothing,
}

fn main() {
    let bar = [1, 2, 3];
    let foos = bar.iter().map(|x| Foo::Value(*x)).collect::<[Foo; 3]>();
}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨:

error[E0277]: the trait bound `[Foo; 3]: std::iter::FromIterator<Foo>` is not satisfied
 --> src/main.rs:8:51
  |
8 |     let foos = bar.iter().map(|x| Foo::Value(*x)).collect::<[Foo; 3]>();
  |                                                   ^^^^^^^ a collection of type `[Foo; 3]` cannot be built from an iterator over elements of type `Foo`
  |
  = help: the trait `std::iter::FromIterator<Foo>` is not implemented for `[Foo; 3]`
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

rust

15
推荐指数
9
解决办法
2万
查看次数

标签 统计

rust ×1