知识卡片

普通读书笔记卡 · 6074.h.1

不安全边界应小且有契约

理解等级:高级

必要的unsafe应集中于最小封装,注明每项操作成立的前提和持续维护的不变量,向外提供可安全使用的接口。额外测试、Miri等工具可发现部分错误,但不能穷尽所有输入与线程交错。安全注释必须说明为何成立,而非只写“这里是安全的”。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 3. Concepts Item 16: Avoid writing unsafe code · 查看原书依据

      Add safety comments that document the preconditions and invariants that the unsafe code relies on. Clippy (Item 29) has a warning to remind you about this. Minimize the amount of code contained in an unsafe block, to limit the potential blast radius of a mistake. Consider enabling the unsafe_op_in_unsafe_fn lint so that explicit unsafe blocks are required wh

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Item 16: Avoid writing unsafe code》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u098-ch03-027)。 - 结论依据:原文在“Add safety comments”附近的定义、例子或代码支撑本卡核心说明:必要的unsafe应集中于最小封装,注明每项操作成立的前提和持续维护的不变量,向外提供可安全使用的接口。边界按已读前提解释;示例未执行。 - 原始内容:Add safety comments that document the preconditions and invariants that the unsafe code relies on. Clippy (Item 29) has a warning to remind you about this. Minimize the amount of code contained in an unsafe block, to limit the potential blast radius of a mistake. Consider enabling the unsafe_op_in_unsafe_fn lint so that explicit unsafe blocks are required wh