知识卡片
避免panic需检查隐式触发点
避免显式panic!还不够,unwrap、expect、unreachable、越界索引及除零等也可能触发panic。对可控输入应选择可失败接口并记录策略;对确实要求不变量的入口,应在文档说明触发条件。自动检测可提示新增风险,但字符串搜索不能证明完全没有panic。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Item 18: Don’t panic · 查看原书依据
slice[index] when the index is out of range x / y when y is zero The second observation around avoiding panics is that a plan that involves constant vigilance of humans is never a good idea. However, constant vigilance of machines is another matter: adding a check to your continuous integration (see Item 32) system that spots new, potentially panicking code
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Item 18: Don’t panic》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u106-ch03-045)。
- 结论依据:原文在“slice[index]”附近的定义、例子或代码支撑本卡核心说明:避免显式panic!还不够,unwrap、expect、unreachable、越界索引及除零等也可能触发panic。边界按已读前提解释;示例未执行。
- 原始内容:slice[index] when the index is out of range x / y when y is zero The second observation around avoiding panics is that a plan that involves constant vigilance of humans is never a good idea. However, constant vigilance of machines is another matter: adding a check to your continuous integration (see Item 32) system that spots new, potentially panicking code