Rust Reference ยท Core Traits
The Copy trait
Reference for the Rust Copy trait: which types are Copy, how to derive it, why it needs Clone, and how copies differ from moves.
Copy is a marker trait for types whose values can be duplicated by copying their bytes. When a type is Copy, using a value (assigning it, passing it to a function, returning it) leaves the original usable, because the compiler copies instead of moving. Integers, floats, bool, char, shared references and tuples or arrays of Copy types are all Copy.
Copy has no methods. It only changes how the compiler treats the type. It is a subtrait of Clone, so every Copy type must also be Clone.
#[derive(Clone, Copy)] struct Point { x: i32, y: i32 }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
pub trait Copy: Clone { }
You almost always get it with #[derive(Clone, Copy)]. A manual implementation is an empty block:
impl Clone for Point { fn clone(&self) -> Self { *self } } impl Copy for Point {}
| Item | Description |
|---|---|
| Supertrait | Clone. A Copy type must implement Clone; the derived clone just copies the value. |
| Methods | None. Copy is a marker. |
| Derive | #[derive(Copy, Clone)] works when every field is Copy. |
Errors: deriving or implementing Copy fails to compile if any field is not Copy (for example a String or Vec<T>), or if the type implements Drop.
Usage notes
- A copy is a plain byte copy. There is no user code that runs, so copying is always cheap relative to the size of the type. Large
Copytypes (a[u8; 4096]) still copy 4096 bytes every time they are used by value. - Copy is a promise about semantics. Add it only for small, plain-data types where having two independent copies makes sense, such as points, colors or ids. Adding
Copyto a public type later is easy; removing it breaks callers. CopyandDropare exclusive. A type with a destructor owns something that must be released exactly once, so it can never beCopy.&TisCopy,&mut Tis not. You can copy as many shared references as you like; a mutable reference is moved (or reborrowed) instead.- Generic bounds:
T: Copylets a function readitems[0]or*rout of a reference without cloning or moving. Without it you would writeT: Cloneand call.clone(). - Closures are
Copywhen everything they capture isCopy(and they capture by shared reference or by copy). - Not the same as
Clone.Cloneis explicit and may be expensive;Copyis implicit and always a bitwise copy. See Copy and Clone. - Deriving adds bounds.
#[derive(Copy)]onstruct W<T>(T)producesimpl<T: Copy> Copy for W<T>.