本文介绍了“特征克隆未被实现”当导出特征Copy for Enum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

#[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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:55