Hi, I wrote a wrapper for Hashset, and tried to compare two set with same elements. But compiler complained == cannot be applied to type HashSet
even if I derived PartialEq(Hashset implement PartialEq and Eq traits).
Compiler suggest add #[derive(Debug, Eq, PartialEq + std::cmp::PartialEq)]
. What's PartialEq + std::cmp::PartialEq
mean? Thanks!
use std::collections::HashSet;
use std::hash::Hash;
#[derive(Debug, PartialEq)]
pub struct CustomSet<T> {
set: HashSet<T>,
}
impl<T> CustomSet<T>
where
T: Eq + PartialEq + Hash + Copy,
{
pub fn new(_input: &[T]) -> Self {
let mut set: HashSet<T> = HashSet::new();
for &i in _input {
set.insert(i);
}
CustomSet { set }
}
}
fn main() {
let set1 = CustomSet::new(&[1, 2, 3]);
let set2 = CustomSet::new(&[1, 2, 3]);
assert_eq!(set1, set2);
/* let mut set1: HashSet<u32> = HashSet::new();
set1.insert(1);
let mut set2: HashSet<u32> = HashSet::new();
set2.insert(1);
assert_eq!(set1, set2);*/
}
error[E0369]: binary operation `==` cannot be applied to type `HashSet<T>`
--> src\lib.rs:6:5
|
6 | set: HashSet<T>,
| ^^^^^^^^^^^^^^^
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
4 | #[derive(Debug, Eq, PartialEq + std::cmp::PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^^