知识卡片

普通读书笔记卡 · 6074.d.1.a.3.a

克隆需尊重资源与不变量

理解等级:中级

clone显式请求按类型定义生成副本,可能执行自定义逻辑;derive通常逐字段克隆。对象若代表唯一访问权,不能仅为方便调用就复制该权利。需要维护实例计数等不变量时可手写Clone;字段不支持Clone则不能直接派生,也不能凭空复制其独占资源。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 2. Traits Clone · 查看原书依据

      Clone The Clone trait indicates that it’s possible to make a new copy of an item, by calling the clone() method. This is roughly equivalent to C++’s copy-constructor but is more explicit: the compiler will never silently invoke this method on its own (read on to the next section for that). Clone can be derived for a type if all of the item’s fields implement

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Clone》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u057-ch02-002)。 - 结论依据:原文在“Clone”附近的定义、例子或代码支撑本卡核心说明:clone显式请求按类型定义生成副本,可能执行自定义逻辑;derive通常逐字段克隆。边界按已读前提解释;示例未执行。 - 原始内容:Clone The Clone trait indicates that it’s possible to make a new copy of an item, by calling the clone() method. This is roughly equivalent to C++’s copy-constructor but is more explicit: the compiler will never silently invoke this method on its own (read on to the next section for that). Clone can be derived for a type if all of the item’s fields implement