← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.h.4 · 生成模型:未记录

不安全代码的证明义务

理解等级:高级

unsafe 允许编译器无法证明安全性的特定操作,包括解引用原始指针、调用 unsafe 函数、访问可变静态量、实现 unsafe trait 和读取 union 字段。其余类型与借用规则继续生效。编写者要验证相关内存和别名条件;将证明限制在小块实现内,并在安全入口完成可执行的检查,可以减少调用方承担的义务。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 高级特性 Unsafe Rust · 查看原书依据

      It’s important to understand that unsafe doesn’t turn off the borrow checker or disable any of Rust’s other safety checks: if you use a reference in unsafe code, it will still be checked. The unsafe keyword only gives you access to these five features that are then not checked by the compiler for memory safety. You’ll still get some degree of safety inside an unsafe block.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第19章 高级特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c19.xhtml`,块 0022) - 结论依据:原文规定五类权限并说明仍检查普通引用,后文强调由程序员承担内存有效性。 - 原始内容:It’s important to understand that unsafe doesn’t turn off the borrow checker or disable any of Rust’s other safety checks: if you use a reference in unsafe code, it will still be checked. The unsafe keyword only gives you access to these five features that are then not checked by the compiler for memory safety. You’ll still get some degree of safety inside an unsafe block.