在我的程序中,我用以下结构表示“事件”:
struct Event {
value: &'static str,
timestamp: usize,
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我曾经PartialEq比较Event变量:大多数时候,Event如果两个相同,我认为它们相等value:
impl PartialEq for Event {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_loose_equality() {
let a = Event { value: "a-value", timestamp: 12345 };
let b = Event { value: "a-value", timestamp: 23456 };
assert_eq!(a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在某些测试中,我想确保两个这样的变量“严格相等”:测试应该失败,它们有不同timestamp(在方面不同Eq)。
根据以下文档assert_eq!:
断言两个表达式彼此相等(使用 PartialEq)。 …