The “trait Clone is is not implemented” when deriving the trait Copy for Enum

后端 未结 2 1399
梦谈多话
梦谈多话 2021-02-19 01:43

The following code:

#[derive(Copy)]
enum MyEnum {
    Test
}

Is giving me this error: error: the trait core::clone::Clone is not i

2条回答
  •  一个人的身影
    2021-02-19 01:57

    The Copy trait is a subtrait of Clone, so you always need to implement Clone if you implement Copy:

    #[derive(Copy, Clone)]
    enum MyEnum {
        Test
    }
    

    This makes sense, as both Copy and Clone are ways of duplicating an existing object, but with different semantics. Copy can duplicate an object by just copying the bits that make up the object (like memcpy in C). Clone can be more expensive, and could involve allocating memory or duplicating system resources. Anything that can be duplicated with Copy can also be duplicated with Clone.

提交回复
热议问题