Alexander Torstling
Alexander Torstling

Reputation: 18908

Subtract UniCase HashSets

I'm trying to run the following code:

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)

And get this error:

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

As far as I can see, the requirement for Sub between HashSets is that the contained type implements Eq + Hash + Clone, which UniCase seems to do. Any pointers?

Upvotes: 3

Views: 296

Answers (1)

Shepmaster
Shepmaster

Reputation: 431669

As you can see from the documentation, Sub is implemented for references to HashMaps:

impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S> 
    where T: Eq + Hash + Clone,
          S: BuildHasher + Default,

Taking explicit references works:

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

There's no need to specify the inner type of s1 or s2 but you do have to clone a as it's moved into the array.

Upvotes: 2

Related Questions