Rust 减法UniCase哈希集
我正在尝试运行以下代码:Rust 减法UniCase哈希集,rust,Rust,我正在尝试运行以下代码: 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().co
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;
}
extern板条箱unicase;
使用unicase::unicase;
使用std::collections::HashSet;
fn main(){
设a=UniCase(“a.to_owned());
设b=UniCase(“b”。to_owned());
让s1:HashSet=[a].iter().cloned().collect();
设s2:HashSet=[a,b].iter().cloned().collect();
设s3=s2-s1;
}
()
并获取以下错误:
error[E0369]:二进制操作“-”不能应用于类型`std::collections::HashSet`
就我所见,hashset
之间的Sub
要求包含的类型实现Eq+Hash+Clone
,而UniCase
似乎就是这样。有什么建议吗 从中可以看出,Sub
是为引用到HashMap
s实现的:
impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S>
where T: Eq + Hash + Clone,
S: BuildHasher + Default,
impl-Sub非常感谢,我自己也看不到这个:)你知道Sub为什么是为了引用而实现的吗?更一般的?@alexandertostlingsub
的实现只是self.difference(rhs).cloned().collect()
(就像您构建的s1
/s2
!)和difference
引用。为什么需要引用,是的,我想这是为了灵活性——拥有引用更常见。如果节省内存是值得的,您可以编写自己的小包装,使用by value迭代器。
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);
}