问题描述
以下代码:
#[derive(Copy)]
enum MyEnum {
Test
}
给我这个错误:
错误:trait core :: clone :: Clone
未实现为 MyEnum
[E0277]
Is giving me this error:error: the trait core::clone::Clone
is not implemented for the type MyEnum
[E0277]
为什么会这样,我该如何解决?
Why is that the case, and how do I fix it?
推荐答案
,所以你总是需要实现克隆
如果你实现复制
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
}
这是有道理的,因为复制
和克隆
是复制现有对象但具有不同语义的方法。 复制
可以通过复制构成对象的位(例如C中的 memcpy
)来复制对象。 克隆
可能更昂贵,可能涉及分配内存或复制系统资源。任何可以与复制
复制的内容也可以与克隆
重复。
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
.
这篇关于“特征克隆未被实现”当导出特征Copy for Enum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!