Collections 创建一个Vec<;b树集<;char>&燃气轮机;来自Vec<;Vec<;char>&燃气轮机;

Collections 创建一个Vec<;b树集<;char>&燃气轮机;来自Vec<;Vec<;char>&燃气轮机;,collections,functional-programming,rust,Collections,Functional Programming,Rust,我试图从Vec创建一个set向量(Vec)。以下是我目前的进展: 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 s

我试图从
Vec
创建一个set向量(
Vec
)。以下是我目前的进展:

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<_>>();
}
使用std::collections::BTreeSet;
fn main(){
//数据
让事务_列表=[
向量!['A','B','C','D',],
向量!['B','B','C'],
向量!['A','B','B','D']
];
//已成功从Vec的Vec创建集。它包含唯一字符
设置项目:BTreeSet=transaction_list.iter().flat_map(| t | t).cloned().collect();
//制作了和Vec相同的Vec。基本上只是实验map和collect
让|same_transaction_list:Vec=transaction_list.iter().map(| t | t).cloned().collect:();
//错误
设置事务:Vec=事务列表
.国际热核实验堆(iter)
.map(| t | t.iter().map(| t | t).cloned().collect())
.cloned().collect::();
}
错误消息是:

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`
错误:没有为类型“&”[E0277]实现trait`core::iter::FromIterator`
.map(| t | t.iter().map(| t | t).cloned().collect())
^~~~~~~~~
帮助:参见E0277的详细说明
注意:`&`类型的集合不能从`char类型元素上的迭代器生成`

我还没有找到用
Vec
制作
Vec
的正确方法。这里是操场url:。

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

let transaction_set: Vec<BTreeSet<_>> =
    transaction_list
    .iter()
    .map(|t| t.iter().cloned().collect())
    .collect();
为了能够调用
克隆
,迭代器
必须是对可克隆内容的引用。但是,您正在尝试迭代
b树集
s,这些树不是引用。编译器选择告诉您,
无法将
字符的内部迭代器
收集到
&
(对某些类型的引用)中,这样就可以满足调用
克隆的要求。我的猜测是,内部类型比
collect
所需的类型有更多的摆动空间。如果我们稍微重写原始代码,使其内部具有更明确的类型:

let transaction_set: Vec<_> =
    transaction_list
    .iter()
    .map(|t| -> BTreeSet<_> {t.iter().cloned().collect()})
    .cloned().collect();
let transaction\u set:Vec=
交易清单
.国际热核实验堆(iter)
.map(|t |->BTreeSet{t.iter().clone().collect()})
.clone().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`
错误:类型不匹配解析`::Output==&u`:
应为结构`collections::btree::set::BTreeSet`,
找到和-ptr[E0271]
.clone().collect();
^~~~~~~~
错误:找不到类型为'core::iter::Cloned,[closure…]>>:core::iter::Iterator的名为'collect'的方法`

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

酷!从没想过我可以在没有地图/过滤器/折叠的情况下调用iter。谢谢你的进一步解释!
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`