知识卡片

普通读书笔记卡 · 6074.e.3.d · 生成模型:未记录

借用以引用传递访问

理解等级:中级

引用允许读取拥有者的值而保留原来的资源责任。只读函数可接收引用,返回计算结果即可。借用结束时,引用本身失效,被引用对象仍由其所有者负责。

相关卡片 [[移动交接释放责任]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 理解所有权 References and Borrowing · 查看原书依据

      The issue with the tuple code in Listing 4-5 is that we have to return the String to the calling function so we can still use the String after the call to calculate_length, because the String was moved into calculate_length. Instead, we can provide a reference to the String value.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第04章 理解所有权(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c04.xhtml`,块 0120) - 结论依据:原文对比移动后再返回 String 与直接用引用计算长度。 - 原始内容:The issue with the tuple code in Listing 4-5 is that we have to return the String to the calling function so we can still use the String after the call to calculate_length, because the String was moved into calculate_length. Instead, we can provide a reference to the String value.