[英]In Rust, how do I create a HashSet from the keys of a HashMap?
[英]How can I create a HashSet from a borrowed array of a generic type in Rust?
我有一个函数采用两个借用的泛型类型T
数组,我想从这些数组创建HashSet
,以便我可以比较它们。
我以为我可以做这样的事情:
pub fn sublist<T: PartialEq>(first_list: &[T], second_list: &[T]) -> bool {
let first_set: HashSet<T> = first_list.iter().collect();
let second_set: HashSet<T> = second_list.iter().collect();
first_set.is_subset(&second_set)
}
但我最终遇到以下错误:
a value of type `std::collections::HashSet<T>` cannot be built from an iterator over elements of type `&T`
value of type `std::collections::HashSet<T>` cannot be built from `std::iter::Iterator<Item=&T>`
help: the trait `std::iter::FromIterator<&T>` is not implemented for `std::collections::HashSet<T>`
由于错误中的第一行,我认为我可以像这样解决它(我只是将 hashset 类型更改为引用&T
):
pub fn sublist<T: PartialEq>(first_list: &[T], second_list: &[T]) -> bool {
let first_set: HashSet<&T> = first_list.iter().collect();
let second_set: HashSet<&T> = second_list.iter().collect();
first_set.is_subset(&second_set)
}
但后来我看到了这些错误:
the trait bound `T: std::cmp::Eq` is not satisfied
the trait `std::cmp::Eq` is not implemented for `T`
note: required because of the requirements on the impl of `std::cmp::Eq` for `&T`
note: required because of the requirements on the impl of `std::iter::FromIterator<&T>` for `std::collections::HashSet<&T>`
我不明白如何从对数组的引用创建新的数据结构。 问题是这些数组被借用的事实,还是最终是PartialEq
上的特征绑定才是问题?
如果出于某种原因,我无法修改函数签名,我该如何使用哈希集来比较集合?
要使用HashSet
您的函数需要具有Eq
和Hash
特征边界:
use std::hash::Hash;
use std::collections::HashSet;
pub fn sublist<T: Eq + Hash>(first_list: &[T], second_list: &[T]) -> bool {
let first_set: HashSet<&T> = first_list.iter().collect();
let second_set: HashSet<&T> = second_list.iter().collect();
first_set.is_subset(&second_set)
}
如果您只知道T
是PartialEq
,那么您可以像这样实现它:
pub fn sublist<T: PartialEq>(first_list: &[T], second_list: &[T]) -> bool {
first_list.iter().all(|v| second_list.contains(v))
}
其他选项包括T: Ord
和使用BTreeSet
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.