知识卡片
断言表达程序前提
assert!(n != 0 && m != 0) 把 GCD 示例要求的前提写在入口,违反时触发 panic,诊断带源码位置。断言在常见的发布构建中仍会检查。debug_assert! 适合只需在开启调试断言时检查的条件。用户输入可能出错时,可在输入边界先返回错误,避免把日常失败变成 panic。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- A Tour of Rust 用最大公约数认识函数 · 查看原书依据
The function’s body starts with a call to the assert! macro, verifying that neither argument is zero. The ! character marks this as
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第01章《A Tour of Rust》|用最大公约数认识函数,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch01.xhtml#rust-functions`
- 结论依据:原文用非零断言说明宏与 panic,并比较 assert! 与 debug_assert!。
- 原始内容:The function’s body starts with a call to the assert! macro, verifying that neither argument is zero. The ! character marks this as