rust•armanriazi•clone•vs•copy
{
Without Copy, Rust applies move semantics to a type’s access.
When using Clone, copying data is explicit.
Until a type implements either Copy or Clone, its internal data cannot be copied.
}
Types can opt into two modes of duplication: cloning and copying.
Cloning (std::clone::Clone)
May be slow and expensive.
Never implicit. A call to the .clone() method is always required.
May differ from original. Crate authors define what cloning means for their types.
---------------------------
Copying (std::marker::Copy)
Always fast and cheap.
Always implicit.
Always identical. Copies are bit-for-bit duplicates of the original value.
So why do Rust programmers not always use Copy? There are three main reasons:
The Copy trait implies that there will only be negligible performance impact. This is true for numbers but not true for types that are arbitrarily large, such as String.
Because Copy creates exact copies, it cannot treat references correctly. Naïvely copying a reference to T would (attempt to) create a second owner of T. That would cause problems later on because there would be multiple attempts to delete T as each reference is deleted.
Some types overload the Clone trait. This is done to provide something similar to, yet different from, creating duplicates. For example, std::rc::Rc<T> uses Clone to create additional references when .clone() is called.
{
Here are some of the types that implement Copy:
All the integer types, such as u32.
The Boolean type, bool, with values true and false.
All the floating point types, such as f64.
The character type, char.
Tuples, if they only contain types that also implement Copy.
For example, (i32, i32) implements Copy(because all of primitive types are fix size so they store on stack)
, but (i32, String) does not.
}
{
concept of copying the pointer, length, and capacity without copying the data probably sounds like making a shallow copy
}
{
Rust has a special annotation called the Copy trait that we can place on types that are stored on the stack like integers. If a type implements the Copy trait, a variable is still valid after assignment to another variable.
}