Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Concepts

Move versus copy in Rust

Reference for Rust move and copy semantics: which types implement Copy, what a move does, when derive(Copy) is allowed, closures and the cost of each choice.

Every assignment, argument pass and return in Rust is a bitwise copy of the value's bytes. What differs is what happens to the source afterwards. For types that implement the Copy marker trait, the source stays valid, so you have two independent values. For all other types the copy is a move: the source is treated as uninitialized and its destructor will not run, so only one owner remains.

Copy is a promise that duplicating the bytes is a complete, harmless duplicate. That is true for numbers and plain data, and false for anything that owns a resource (a heap allocation, a file, a lock guard), where duplicating the bytes would produce two owners of the same resource.

Rust
let n = 7;       // i32 is Copy
let m = n;       // both usable
let s = String::from("x");
let t = s;       // String is not Copy: s is moved

Try it

Rust Which types are Copy?

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 Deriving Copy for your own types

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 Closures can be Copy too

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.

Rules for Copy

  • Primitive scalars (i8..i128, u8..u128, isize, usize, f32, f64, bool, char), shared references &T, raw pointers, function pointers, ! and () are Copy.
  • Tuples, arrays and closures are Copy exactly when all their elements (or captured values) are Copy. A closure that captures by shared reference is Copy; one that captures a String by move is not.
  • Never Copy: &mut T, String, Vec<T>, Box<T>, Rc<T>, Arc<T>, File, mutex guards.
  • Deriving: #[derive(Clone, Copy)] is allowed only if every field is Copy. Copy requires Clone as a supertrait, because x.clone() on a Copy type must agree with a plain copy.
  • Not with Drop: a type that implements Drop can never be Copy, since a bit copy would drop twice.
  • Generic structs: #[derive(Copy)] adds T: Copy bounds, so Wrapper<String> is not Copy.

Usage notes

  • Moving is cheap. A move copies only the value's inline bytes (a Vec moves 24 bytes), never the heap data. clone() is what duplicates the heap contents, and it can be expensive.
  • Big Copy types are still copied on every use. A [u8; 1_000_000] passed by value copies a megabyte; pass & instead. Small types (up to a few words) are best passed by value.
  • Copy is a semver-visible promise. Removing Copy from a public type breaks users. Adding it is non-breaking, but it cannot be undone, so only derive it for plain data types you will not change into resource owners.
  • The compiler can only move out of owned values. Indexing v[0] on a Vec<String> is an error ("cannot move out of index"), while the same on a Vec<i32> copies.
  • Clone without Copy is normal. String is Clone but not Copy; clone() makes the duplication visible in the source.
  • After a move you can re-initialize. A mut binding that was moved from can be assigned a new value and used again.