从 Vec<Vec<char>> 创建 Vec<BTreeSet<char>>

ril*_*lut 5 collections functional-programming rust

Vec<BTreeSet<char>>我正在尝试从 中创建一个 set ( )向量Vec<Vec<char>>。这是我迄今为止的进展:

use std::collections::BTreeSet;

fn main() {
    // The data
    let transaction_list = [
        vec!['A','B','C','D'],
        vec!['B','B','C'],
        vec!['A','B','B','D']
    ];

    // Successfully created a set from the Vec of Vec. It contains unique chars
    let item_set: BTreeSet<char> = transaction_list.iter().flat_map(|t| t).cloned().collect();

    // Made the same Vec of Vec. Basically just experimenting with map and collect
    let the_same_transaction_list: Vec<Vec<char>> = transaction_list.iter().map(|t| t ).cloned().collect::<Vec<_>>();

    // ERROR
    let transaction_set: Vec<BTreeSet<char>> = transaction_list
                                                .iter()
                                                .map(|t| t.iter().map(|t| t).cloned().collect() )
                                                .cloned().collect::<Vec<_>>();
}
Run Code Online (Sandbox Code Playgroud)

错误消息是:

error: the trait `core::iter::FromIterator<char>` is not implemented for the type `&_` [E0277]
                                      .map(|t| t.iter().map(|t| t).cloned().collect() )
                                                                            ^~~~~~~~~
help: see the detailed explanation for E0277
note: a collection of type `&_` cannot be built from an iterator over elements of type `char`
Run Code Online (Sandbox Code Playgroud)

我还没有找到正确的方法来Vec<BTreeSet<char>>制作Vec<Vec<char>>. 这是游乐场网址: http: //is.gd/WVONHY

She*_*ter 4

错误消息有点奇怪。这是解决方案:

let transaction_set: Vec<BTreeSet<_>> =
    transaction_list
    .iter()
    .map(|t| t.iter().cloned().collect())
    .collect();
Run Code Online (Sandbox Code Playgroud)

两个主要变化是:

  1. map(|x| x)毫无意义。这是一个无操作。
  2. 我把外面的去掉了cloned。调用的结果类型map已经是 a BTreeSet。无需再次克隆它。

后者解决了您的问题,所以让我们看一下定义:

fn cloned<'a, T>(self) -> Cloned<Self> 
    where Self: Iterator<Item=&'a T>,
          T: 'a + Clone
Run Code Online (Sandbox Code Playgroud)

为了能够调用cloned,迭代器Item必须是对可克隆对象的引用。但是,您正在尝试迭代BTreeSets,这不是引用。编译器选择告诉您没有办法将collect内部迭代器 ofchar转换为 a &_(对某种类型的引用),从而满足调用 的要求cloned。我的猜测是,内部类型比 所需的类型有更多的回旋空间collect。如果我们稍微重写一下原始代码以在内部拥有更明确的类型:

let transaction_set: Vec<_> =
    transaction_list
    .iter()
    .map(|t| -> BTreeSet<_> {t.iter().cloned().collect()})
    .cloned().collect();
Run Code Online (Sandbox Code Playgroud)

我们得到一组不同的错误:

error: type mismatch resolving `<[closure...] as core::ops::FnOnce<(&collections::vec::Vec<char>,)>>::Output == &_`:
 expected struct `collections::btree::set::BTreeSet`,
    found &-ptr [E0271]
         .cloned().collect();
          ^~~~~~~~

error: no method named `collect` found for type `core::iter::Cloned<core::iter::Map<core::slice::Iter<'_, collections::vec::Vec<char>>, [closure...]>>` in the current scope
         .cloned().collect();
                   ^~~~~~~~~
note: the method `collect` exists but the following trait bounds were not satisfied: `core::iter::Cloned<core::iter::Map<core::slice::Iter<'_, collections::vec::Vec<char>>, [closure...]>> : core::iter::Iterator`
Run Code Online (Sandbox Code Playgroud)

这有助于强调问题是由 的外部使用引起的cloned