从Vec<Vec<char>>创建一个 Vec<BTreeSet<char>>。

5

我正在尝试从 Vec<Vec<char>>创建一个 Vec<BTreeSet<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<_>>();
}

错误信息如下:
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`

我还没有找到制作 Vec<BTreeSet<char>> 的正确方法,它是由 Vec<Vec<char>> 构成的。这里是 playground 的链接:http://is.gd/WVONHY

1个回答

4
错误信息有点奇怪。以下是解决方法:
let transaction_set: Vec<BTreeSet<_>> =
    transaction_list
    .iter()
    .map(|t| t.iter().cloned().collect())
    .collect();

这两个主要的变化是:

  1. map(|x| x) 是无意义的。它是一个空操作。
  2. 我删除了外部的 clonedmap 调用的结果类型已经是 BTreeSet,因此没有必要再次克隆它。

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

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

要调用cloned,迭代器Item必须是一个可克隆的引用。但是,您正在尝试迭代BTreeSet,而它们不是引用。编译器选择告诉您没有办法将char的内部迭代器collect&_(某种类型的引用),这将满足调用cloned的要求。我猜测,内部类型比collect所需的类型更灵活。如果我们稍微改写原始代码,使其在内部具有更明确的类型:
let transaction_set: Vec<_> =
    transaction_list
    .iter()
    .map(|t| -> BTreeSet<_> {t.iter().cloned().collect()})
    .cloned().collect();

我们会得到一组不同的错误:
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`

这有助于突出问题是由外部使用 cloned 导致的。


1
酷!从未想过我可以在不使用map/filter/fold的情况下调用iter。感谢您的进一步解释! - luthfianto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接