知识卡片
Copy与Clone表达复制
实现 Copy 的值可隐式复制,原绑定仍有效。需要析构的类型不能这样做。用 clone 表达显式复制时,具体成本与共享方式由实现决定。String 会复制内容,而有些引用计数句柄只增加共享计数。
相关卡片 [[移动交接释放责任]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 理解所有权 Variables and Data Interacting with Move · 查看原书依据
Rust has a special annotation called the Copy trait that we can place on types that are stored on the stack, as integers are (we’ll talk more about traits in Chapter 10). If a type implements the Copy trait, variables that use it do not move, but rather are trivially copied, making them still valid after assignment to another variable.
参考来源
- 位置:《The Rust Programming Language(第2版)》第04章 理解所有权(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c04.xhtml`,块 0093)
- 结论依据:原文解释 Copy 使赋值后旧值仍可用,前文用 String::clone 示范显式复制。
- 原始内容:Rust has a special annotation called the Copy trait that we can place on types that are stored on the stack, as integers are (we’ll talk more about traits in Chapter 10). If a type implements the Copy trait, variables that use it do not move, but rather are trivially copied, making them still valid after assignment to another variable.