将Rust中的自定义结构vecs的vec展平

时间:2018-11-07 15:35:50

标签: rust

我有一个自定义结构,经过一些处理,我得到了一个包含该结构矢量的向量。但是我只能获取结构的平面向量。

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Verse {
    pub book_id: i16,
    pub chapter: i16,
    pub verse: i16,
    pub text: String,
}

fn main() {
    let v1 = Verse {
        book_id: 1,
        chapter: 1,
        verse: 1,
        text: String::from("a"),
    };
    let v2 = Verse {
        book_id: 2,
        chapter: 1,
        verse: 1,
        text: String::from("b"),
    };
    let vv = [[v1], [v2]];
    let flatten = vv.iter().flatten().collect::<Vec<Verse>>();
}

错误:

error[E0277]: a collection of type `std::vec::Vec<Verse>` cannot be built from an iterator over elements of type `&Verse`
  --> src/main.rs:23:39
   |
23 |     let flatten = vv.iter().flatten().collect::<Vec<Verse>>();
   |                                       ^^^^^^^ a collection of type `std::vec::Vec<Verse>` cannot be built from `std::iter::Iterator<Item=&Verse>`
   |
   = help: the trait `std::iter::FromIterator<&Verse>` is not implemented for `std::vec::Vec<Verse>``std::vec::Vec<Verse>`

但是我找不到如何提出解决方案的例子。

0 个答案:

没有答案