知识卡片
借用有效范围的约束
借用检查器同时看两个方向。所指数据的有效范围给引用设置上限,程序使用该引用的位置给它设置下限。若在内层块借用局部 x,并在块外用 r 解引用,则下限超过上限,编译器会拒绝。把使用移回内层或让所指的数据活得更久都能消除这个冲突。借用容器元素时还须检查其所有者的有效性,存放引用的数据结构也会将使用约束传到字段引用。生命周期参与编译时检查,无须额外的运行时计时。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- References 从所指值和使用点推导生命周期 · 查看原书依据
The first kind of constraint limits how large a reference’s lifetime can be, while the second kind limits how small it can be.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第04章《References》|从所指值和使用点推导生命周期,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch04.xhtml#borrowing-a-local-variable`
- 结论依据:原文用局部块的错误和可行示例配图展示上下限,并将同一份推理推广到容器的元素与引用字段。
- 原始内容:The first kind of constraint limits how large a reference’s lifetime can be, while the second kind limits how small it can be.