知识卡片

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

调用不安全函数的契约

理解等级:中级

unsafe fn 表示调用者需满足编译器无法验证的前提,例如地址指向有效内存、覆盖长度合适、指定时间内不存在互相冲突的访问。调用处的 unsafe 声明自己已完成这些核验。若包装函数能验证全部前提,或根据其安全参数证明前提始终成立,就可提供安全签名。

相关卡片 [[以边界检查封装切片]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 高级特性 Calling an Unsafe Function or Method · 查看原书依据

      The second type of operation you can perform in an unsafe block is calling unsafe functions. Unsafe functions and methods look exactly like regular functions and methods, but they have an extra unsafe before the rest of the definition. The unsafe keyword in this context indicates the function has requirements we need to uphold when we call this function, because Rust can’t guarantee we’ve met these requirements.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第19章 高级特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c19.xhtml`,块 0050) - 结论依据:原文将 unsafe 函数定义为带有编译器无法检查的调用要求,并说明调用者承担遵守文档合同的义务。 - 原始内容:The second type of operation you can perform in an unsafe block is calling unsafe functions. Unsafe functions and methods look exactly like regular functions and methods, but they have an extra unsafe before the rest of the definition. The unsafe keyword in this context indicates the function has requirements we need to uphold when we call this function, because Rust can’t guarantee we’ve met these requirements.