知识卡片
按值调用可转移所有权
追踪非 Copy 类型时,关注返回值、参数、字段和赋值目标。to_string 返回的新 String 可移进 Person 的 name,push 接收 Person 后,向量也间接拥有 name 的文本。赋值到一个仍持有旧值的变量会先销毁旧值;旧值已经移动时,重新赋值只是恢复该变量的初始化状态。移动体现的是责任交接,底层可直接写入目标位置。
关联阅读:[[移动交接销毁责任]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Ownership and Moves 移动经过赋值、构造、函数参数和返回值 · 查看原书依据
Passing arguments to functions moves ownership to the function’s parameters; returning a value from a function moves ownership to the caller.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第03章《Ownership and Moves》|移动经过赋值、构造、函数参数和返回值,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch03.xhtml#more-operations-that-move`
- 结论依据:原文对比覆盖已初始化变量与重新填充被移动变量,并用 Person 例子追踪跨调用的所有权。
- 原始内容:Passing arguments to functions moves ownership to the function’s parameters; returning a value from a function moves ownership to the caller.