← 返回刚才阅读的位置

知识卡片

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

移动交接释放责任

理解等级:中级

把一个 String 移到另一个绑定时,堆上的字节不需要复制,接收者承担清理责任。原绑定失效,因而两个名字不能分别释放同一缓冲。按值参数可令所有权进入函数,返回值则可把所有权交给调用者。

相关卡片 [[所有者离域触发析构]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 理解所有权 Variables and Data Interacting with Move · 查看原书依据

      If you’ve heard the terms shallow copy and deep copy while working with other languages, the concept of copying the pointer, length, and capacity without copying the data probably sounds like making a shallow copy. But because Rust also invalidates the first variable, instead of being called a shallow copy, it’s known as a move. In this example, we would say that s1 was moved into s2.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第04章 理解所有权(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c04.xhtml`,块 0077) - 结论依据:原文说明复制 String 描述信息后旧绑定失效,并以双重释放解释约束。 - 原始内容:If you’ve heard the terms shallow copy and deep copy while working with other languages, the concept of copying the pointer, length, and capacity without copying the data probably sounds like making a shallow copy. But because Rust also invalidates the first variable, instead of being called a shallow copy, it’s known as a move. In this example, we would say that s1 was moved into s2.