知识卡片

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

返回借用受共同范围约束

理解等级:高级

签名 longest<‘a>(x: &‘a str, y: &‘a str) -> &‘a str 表示可能返回任一输入的借用,返回值只能在两份输入都有效的范围内使用。两份数据无需具有同样长的实际存活时间。标注约束调用点的使用,不能延长数据的存活。若输出只会来自 x,可仅把 x 与输出相连。

相关卡片 [[借用检查阻止悬垂引用]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第10章 泛型类型、trait 与生命周期(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c10.xhtml`,块 0246) - 结论依据:原文说明共同参数绑定到两借用的重叠范围,并用内层短借用约束结果。 - 原始内容:When we pass concrete references to longest, the concrete lifetime that is substituted for 'a is the part of the scope of x that overlaps with the scope of y. In other words, the generic lifetime 'a will get the concrete lifetime that is equal to the smaller of the lifetimes of x and y.