知识卡片

普通读书笔记卡 · 6075.d.8 · 生成模型:未记录

比较断言保留两侧值

理解等级:中级

assert_eq! 和 assert_ne! 分别检查相等与不等。失败时报告按 left、right 给出两个参数的值。两类断言依赖 PartialEq 比较和 Debug 格式化。自定义类型可在适用时派生这两个 trait。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 编写自动化测试 Checking Results with the assert! Macro · 查看原书依据

      Under the surface, the assert_eq! and assert_ne! macros use the operators == and !=, respectively. When the assertions fail, these macros print their arguments using debug formatting, which means the values being compared must implement the PartialEq and Debug traits. All primitive types and most of the standard library types implement these traits.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第11章 编写自动化测试(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c11.xhtml`,块 0087) - 结论依据:两个宏采用的比较运算符和诊断格式决定了类型需要实现的 trait。 - 原始内容:Under the surface, the assert_eq! and assert_ne! macros use the operators == and !=, respectively. When the assertions fail, these macros print their arguments using debug formatting, which means the values being compared must implement the PartialEq and Debug traits. All primitive types and most of the standard library types implement these traits.