知识卡片

普通读书笔记卡 · 6074.f.9.a

共同生命周期是有效区间约束

理解等级:中级

两个输入都标’a、输出也标’a,意味着返回引用的使用必须落在两输入都有效的区域,并非对象有完全相同出生与析构时刻。smaller可能返回任一输入,因此调用者不能让结果活过较短的借用。若返回只来自一边,应避免多余绑定。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 3. Concepts Algebra of Lifetimes · 查看原书依据

      smaller<'a>(left: &'a Item, right: &'a Item) -> &'a Item { // ... } This appears to imply that the two input lifetimes are the “same,” but the scare quotes (here and previously) are included to signify that that’s not quite what’s going on. The raison d’être of lifetimes is to ensure that references to items don’t outlive the items themselves; with this in m

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Algebra of Lifetimes》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u081-ch03-006)。 - 结论依据:原文在“smaller”附近的定义、例子或代码支撑本卡核心说明:两个输入都标'a、输出也标'a,意味着返回引用的使用必须落在两输入都有效的区域,并非对象有完全相同出生与析构时刻。边界按已读前提解释;示例未执行。 - 原始内容:smaller<'a>(left: &'a Item, right: &'a Item) -> &'a Item { // ... } This appears to imply that the two input lifetimes are the “same,” but the scare quotes (here and previously) are included to signify that that’s not quite what’s going on. The raison d’être of lifetimes is to ensure that references to items don’t outlive the items themselves; with this in m