知识卡片

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

借用检查阻止悬垂引用

理解等级:中级

引用在使用期间必须指向仍然有效的数据。比如外层变量保存一个内层局部值的引用,内层作用域结束后读取外层引用就会被拒绝。调整所有者的作用域,或在它释放之前完成借用的所有使用,才能消除这一错误。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第10章 泛型类型、trait 与生命周期(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c10.xhtml`,块 0210) - 结论依据:原文用内外块的有效范围解释为什么借用检查拒绝外层引用。 - 原始内容:Here, we’ve annotated the lifetime of r with 'a and the lifetime of x with 'b. As you can see, the inner 'b block is much smaller than the outer 'a lifetime block. At compile time, Rust compares the size of the two lifetimes and sees that r has a lifetime of 'a but that it refers to memory with a lifetime of 'b.