Skip to content
Tutorials Center
My learning

Topics

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.

Rust
#[derive(Clone, Copy)]
struct Point { x: i32, y: i32 }

Try it

Rust Copy versus move

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.

Rust Copy in generic code

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

Rust
pub trait Copy: Clone { }

You almost always get it with #[derive(Clone, Copy)]. A manual implementation is an empty block:

Rust
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 Copy types (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 Copy to a public type later is easy; removing it breaks callers.
  • Copy and Drop are exclusive. A type with a destructor owns something that must be released exactly once, so it can never be Copy.
  • &T is Copy, &mut T is not. You can copy as many shared references as you like; a mutable reference is moved (or reborrowed) instead.
  • Generic bounds: T: Copy lets a function read items[0] or *r out of a reference without cloning or moving. Without it you would write T: Clone and call .clone().
  • Closures are Copy when everything they capture is Copy (and they capture by shared reference or by copy).
  • Not the same as Clone. Clone is explicit and may be expensive; Copy is implicit and always a bitwise copy. See Copy and Clone.
  • Deriving adds bounds. #[derive(Copy)] on struct W<T>(T) produces impl<T: Copy> Copy for W<T>.