gpt4 book ai didi

rust - 派生 PartialEq 时排除字段

转载 作者:行者123 更新时间:2023-11-29 07:50:12 37 4
gpt4 key购买 nike

是否有一种简单的方法来注释结构中的字段,以便在派生 PartialEq 特征时忽略它们?例如:

#[derive(PartialEq,Eq)]
pub struct UndirectedGraph {
nodes: HashMap<NodeIdx, UndirectedNode>,
// mapping of degree to nodes of that degree
degree_index: Vec<HashSet<NodeIdx>>,
}

我希望两个无向图在具有相同的 nodes 字段时被视为相等,但 degree_index 字段可能不同(向量可能包含额外的空哈希集最后)。

显然我可以手动实现特征,但自动推导会更简单。

最佳答案

检查派生创建 ( docs )。它提供了比标准库版本更强大的备用 derive 宏,包括忽略 Hash 字段的方法。和 PartialEq特质。

关于rust - 派生 PartialEq 时排除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41009481/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com