是否有一种简单的方法来注释结构中的字段,以便在导出PartialEq特征时忽略它们?例如:
#[derive(PartialEq,Eq)]
pub struct UndirectedGraph {
nodes: HashMap<NodeIdx, UndirectedNode>,
// mapping of degree to nodes of that degree
degree_index: Vec<HashSet<NodeIdx>>,
}
Run Code Online (Sandbox Code Playgroud)
我希望两个无向图在它们具有相同的nodes字段时被认为是相等的,但是degree_index字段可能不同(向量可能在末尾包含额外的空哈希集).
显然我可以手动实现特性,但自动推导会更简单.
derivative如果您需要更复杂的形式,我还会推荐该板条箱#[derive]。
如果您只需要这样的一两次,那么手动实现所需的特征可能比派生它们更容易。 PartialEq并且Eq很容易自己实现:
impl PartialEq for UndirectedGraph {
fn eq(&self, other: &Self) -> bool {
self.nodes == other.nodes
}
}
impl Eq for UndirectedGraph {}
Run Code Online (Sandbox Code Playgroud)