最小示例:
use std::collections::HashSet;
struct Something {
a: HashSet<Point>,
b: HashSet<Point>
}
impl Something {
fn TEST(&self) {
let new = self.a | self.b;
}
}
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
struct Point {
x: usize,
y: usize
}
检查它on the Rust Playground。如果您尝试编译此代码,Rust将抱怨error[E0369]: no implementation for std::collections::HashSet<Point> | std::collections::HashSet<Point>
。
但是根据HashSet的the docs,至少根据我的理解,应该为HashSet实现BitOr
特征,其中T: Eq + Hash + Clone
,此处显然是Point
。那么实际情况如何,如何解决?
仔细看一下implemention of BitOr
for HashSet
:
BitOr
[HashSet
仅实现对impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S>
where
...
的引用,不针对拥有的值。
如下重写您为BitOr
实现的实现将按预期方式进行编译。
HashSet
注意,我们使用对Something::TEST
和impl Something {
fn TEST(&self) {
let new = &self.a | &self.b;
}
}
的引用。