我正在尝试从 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。