知识卡片
派生复制逐字段传播
派生 Clone 时,复合类型的克隆由各字段的实现决定。切片转为拥有元素的向量可能需要克隆每项。只有全部组成也能 Copy 且类型符合规则时,才可用隐式复制。字段的克隆既可能复制内容也可能保留共享资源。
相关卡片 [[Copy与Clone表达复制]]、[[移动交接释放责任]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 附录 C · 可派生的 trait Clone and Copy for Duplicating Values · 查看原书依据
Deriving Clone implements the clone method, which when implemented for the whole type, calls clone on each of the parts of the type. This means all the fields or values in the type must also implement Clone to derive Clone.
参考来源
- 位置:《The Rust Programming Language(第2版)》附录 C · 可派生的 trait(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/b03.xhtml`,块 0033)
- 结论依据:原文说明派生 Clone 会对每部分调用 clone,并以切片转向量为用途。
- 原始内容:Deriving Clone implements the clone method, which when implemented for the whole type, calls clone on each of the parts of the type. This means all the fields or values in the type must also implement Clone to derive Clone.