← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 10504.a.6 · 生成模型:未记录

移动交接销毁责任

理解等级:中级

对 Vec 执行 let t = s,t 接管向量并最终负责释放元素和缓冲区。s 此时处于未初始化状态,let u = s 会被拒绝。移动只涉及值本身,在这里是向量的三个头字段,堆上的元素和文本无需搬迁。需要独立的副本时,调用 s.clone() 会复制向量及其中的 String;需要共享寿命时,可以建立引用计数所有者。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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#moves` - 结论依据:原文对比 Python 别名、C++ vector 左值深拷贝和 Rust 按值移动,用编译报错解释了移动源的状态。 - 原始内容:The source relinquishes ownership of the value to the destination and becomes uninitialized; the destination now controls the value’s lifetime.