减去UniCase HashSets

时间:2017-04-19 18:50:56

标签: 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().collect();
    let s2: HashSet<UniCase<String>> = [a, b].iter().cloned().collect();
    let s3 = s2 - s1; 
}

Playground

并收到此错误:

error[E0369]: binary operation `-` cannot be applied to type `std::collections::HashSet<unicase::UniCase<std::string::String>>`

据我所知,Sub之间HashSets的要求是所包含的类型实现了Eq + Hash + Clone,而UniCase似乎也是如此。有什么指针吗?

1 个答案:

答案 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);
}

无需指定s1s2的内部类型,但您必须克隆a,因为它已移入数组。< / p>