我试图运行以下代码:
extern crate unicase;
use unicase::UniCase;
use std::collections::HashSet;
fn main() {
let a = UniCase("a".to_owned());
let b = UniCase("b".to_owned());
let s1: HashSet<UniCase<String>> = [a].iter().cloned().collect();
let s2: HashSet<UniCase<String>> = [a, b].iter().cloned().collect();
let s3 = s2 - s1;
}
并收到此错误:
error[E0369]: binary operation `-` cannot be applied to type `std::collections::HashSet<unicase::UniCase<std::string::String>>`
据我所知,Sub
之间HashSets
的要求是所包含的类型实现了Eq + Hash + Clone
,而UniCase
似乎也是如此。有什么指针吗?
答案 0 :(得分:1)
正如您从the documentation所看到的,Sub
已针对引用实施为HashMap
:
impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S>
where T: Eq + Hash + Clone,
S: BuildHasher + Default,
明确参考作品:
extern crate unicase;
use unicase::UniCase;
use std::collections::HashSet;
fn main() {
let a = UniCase("a".to_owned());
let b = UniCase("b".to_owned());
let s1: HashSet<_> = [a.clone()].iter().cloned().collect();
let s2: HashSet<_> = [a, b].iter().cloned().collect();
let s3 = &s2 - &s1;
println!("{:?}", s3);
}
无需指定s1
或s2
的内部类型,但您必须克隆a
,因为它已移入数组。< / p>