← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.c.10 · 生成模型:未记录

更新结构体会移动字段

理解等级:中级

结构体更新 New { field: value, ..old } 为剩余字段从旧实例取值。Copy 字段可复制,拥有资源的非 Copy 字段会移动。移动某字段后不能继续使用旧实例整体,但未被移走的字段在规则允许时仍可单独使用。

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

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第05章 用结构体组织相关数据(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c05.xhtml`,块 0040) - 结论依据:原文比较更新时使用 String 与使用 Copy 字段对旧对象的影响。 - 原始内容:Note that the struct update syntax uses = like an assignment; this is because it moves the data, just as we saw in “Variables and Data Interacting with Move” on page 64. In this example, we can no longer use user1 after creating user2 because the String in the username field of user1 was moved into user2.