Rust抱怨执行联合时没有为HashSet实现BitOr,文档指出应该是

问题描述 投票:0回答:1

最小示例:

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。那么实际情况如何,如何解决?

rust hashset
1个回答
0
投票

仔细看一下implemention of BitOr for HashSet

BitOr

[HashSet仅实现对impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S> where ... 的引用,不针对拥有的值。

如下重写您为BitOr实现的实现将按预期方式进行编译。

HashSet

注意,我们使用对Something::TESTimpl Something { fn TEST(&self) { let new = &self.a | &self.b; } } 的引用。

© www.soinside.com 2019 - 2024. All rights reserved.