知识卡片

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

Copy改变赋值与传参语义

理解等级:中级

Copy表示按位复制即可产生有效新值,使赋值和传参可保留原值,而非转移后禁止继续使用。Copy要求Clone,但隐式复制不会调用clone中的自定义代码。是否实现Copy会影响类型的使用方式,是公共语义选择,不只是少写一个方法。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

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

      pub trait Copy: Clone { } There are no methods in this trait, meaning that it is a marker trait (as described in Item 2): it’s used to indicate some constraint on the type that’s not directly expressed in the type system. In the case of Copy, the meaning of this marker is that a bit-for-bit copy of the memory holding an item gives a correct new item. Effecti

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Copy》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u058-ch02-003)。 - 结论依据:原文在“pub trait Copy: Clone”附近的定义、例子或代码支撑本卡核心说明:Copy表示按位复制即可产生有效新值,使赋值和传参可保留原值,而非转移后禁止继续使用。边界按已读前提解释;示例未执行。 - 原始内容:pub trait Copy: Clone { } There are no methods in this trait, meaning that it is a marker trait (as described in Item 2): it’s used to indicate some constraint on the type that’s not directly expressed in the type system. In the case of Copy, the meaning of this marker is that a bit-for-bit copy of the memory holding an item gives a correct new item. Effecti