复制和克隆有什么区别? [英] What is the difference between Copy and Clone?

查看:55
本文介绍了复制和克隆有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎暗示它只是一个实现细节(memcpy vs ???),但我找不到任何关于差异的明确描述.

This issue seems to imply it's just an implementation detail (memcpy vs ???), but I can't find any explicit description of the differences.

推荐答案

Clone 专为任意重复而设计:T 类型的Clone 实现code> 可以执行创建新 T 所需的任意复杂操作.这是一个正常的 trait(除了在前奏中),因此需要像普通 trait 一样使用,带有方法调用等.

Clone is designed for arbitrary duplications: a Clone implementation for a type T can do arbitrarily complicated operations required to create a new T. It is a normal trait (other than being in the prelude), and so requires being used like a normal trait, with method calls, etc.

Copy trait 表示可以通过 memcpy 安全复制的值:诸如重新分配和按值将参数传递给函数之类的东西总是 memcpys,所以对于 Copy 类型,编译器明白它不需要考虑那些 一个动作.

The Copy trait represents values that can be safely duplicated via memcpy: things like reassignments and passing an argument by-value to a function are always memcpys, and so for Copy types, the compiler understands that it doesn't need to consider those a move.

这篇关于复制和克隆有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆