← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.f

引用必须保持有效与存活

理解等级:中级

Rust引用要求目标值有效、对齐,并在引用被使用期间存活。返回局部栈变量的引用违反这个条件,不能靠标成’static延长实际对象寿命。安全Rust的检查帮助维持契约;来自unsafe代码的引用同样必须满足契约,不能以编译通过替代实现者证明。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Rust References · 查看原书依据

      Rust References The most ubiquitous pointer-like type in Rust is the reference, with a type that is written as &T for some type T. Although this is a pointer value under the covers, the compiler ensures that various rules around its use are observed: it must always point to a valid, correctly aligned instance of the relevant type T, whose lifetime (Item 14)

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Rust References》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u040-ch01-053)。 - 结论依据:原文在“Rust References”附近的定义、例子或代码支撑本卡核心说明:Rust引用要求目标值有效、对齐,并在引用被使用期间存活。边界按已读前提解释;示例未执行。 - 原始内容:Rust References The most ubiquitous pointer-like type in Rust is the reference, with a type that is written as &T for some type T. Although this is a pointer value under the covers, the compiler ensures that various rules around its use are observed: it must always point to a valid, correctly aligned instance of the relevant type T, whose lifetime (Item 14)