skip to content

In Rust, which types can implement `Copy`, and why does `#[derive(Clone, Copy)]` fail on a struct with a `String` field?

level: middleimportance: must knowfreq 65%

answer

  1. every component must qualify
  2. shared versus mutable references
  3. Clone is a supertrait
  4. E0204 names the offending field

basics

~20 s

A type can be Copy only if every field is Copy and it has no Drop impl: numbers, bool, char, shared references, raw and function pointers, and tuples or arrays of Copy types. A String field is not Copy, so rustc reports E0204.

solid answer

~40 s

rustc accepts `impl Copy` only when every field, and for an enum every field of every variant, is itself `Copy`, and the type has no `Drop` impl. Primitives, `&T` for any `T`, raw pointers, function pointers, and tuples, arrays and `Option`s of `Copy` types qualify. `&mut T` never does, because a copy would create two live mutable references, and `String`, `Vec<T>` and `Box<T>` do not, because they own heap memory. So `#[derive(Clone, Copy)]` on a struct with a `name: String` fails with E0204, "the trait `Copy` cannot be implemented for this type", pointing at the field. Because `Clone` is a supertrait of `Copy`, `#[derive(Copy)]` alone fails with E0277. On a generic struct the derive emits `impl<T: Copy> Copy`, so `Tagged<char>` is `Copy` and `Tagged<String>` is only `Clone`.

code

rust · 6 lines
rust
#[derive(Clone, Copy)]
struct Sprite {
    name: String,
    x: f32,
}
fn main() {}

go deeper

for a junior

Recall the short list: numbers, bool, char, shared references, and tuples or arrays of those are Copy; String, Vec and Box are not.

for a middle

Explain the structural rule (every field Copy, no Drop, Clone as a supertrait) and read E0204 and E0277 aloud, naming the field each one blames.

for a senior

Show you know the derive's T: Copy bound on generics, when it is too strict, and how a hand-written impl with clone returning *self removes it.

for a principal

Treat Copy as a public API promise: decide which value types may commit to staying plain data, since removing Copy later breaks callers.

## The rule rustc checks `Copy` means "duplicating this value's bytes produces a complete, independent value". rustc therefore checks three conditions before it accepts an `impl Copy`, whether written by hand or generated by `#[derive(Copy)]`: 1. **Every component is `Copy`.** For a struct that means every field; for an enum, every field of every variant; for a union, every variant. 2. **The type has no `Drop` implementation.** A destructor means the type manages something beyond its own bytes (rustc reports E0184 if you try). 3. **The type implements `Clone`**, because the trait is declared `pub trait Copy: Clone`. The first rule is the one people hit most. The standard library documentation puts it simply: "A type can implement `Copy` if all of its components implement `Copy`." ## Which standard types are Copy | Type | Copy? | Why | |---|---|---| | `i32`, `u8`, `f64`, `bool`, `char` | yes | plain bytes | | `&T` (any `T`) | yes | copies the reference, not the referent | | `&mut T` | never | a copy would alias a mutable reference | | `*const T`, `*mut T`, `fn()` pointers | yes | plain addresses | | `[T; N]`, tuples, `Option<T>` | when the parts are | structural | | `String`, `Vec<T>`, `Box<T>` | no | own a heap allocation | | closures | when every captured value is | structural | | `std::ops::Range<u32>` | no | the standard library does not implement it | Two entries surprise people: - A struct that holds `&'a String` **can** be `Copy` even though `String` is not, because copying it copies only the reference. - A `[u8; 64]` array is `Copy`: there is no size limit, only the ownership rule. ## Reading E0204 and E0277 Deriving `Copy` on a sprite that owns its name fails at the field: ```plaintext error[E0204]: the trait `Copy` cannot be implemented for this type 2 | struct Sprite { | ^^^^^^ 3 | name: String, | ------------ this field does not implement `Copy` ``` The error index explains it the same way: `Copy` "was implemented on a type which contains a field that doesn’t implement the `Copy` trait". The index's second example is a struct holding `&'a mut bool`, which fails because "`&mut T` is not `Copy`, even when `T` is `Copy`". Forgetting `Clone` produces a different error. `#[derive(Copy)]` on `Vec2` alone gives E0277, "the trait bound `Vec2: Clone` is not satisfied", with a note pointing at the `Copy: Clone` supertrait bound. The fix is `#[derive(Clone, Copy)]`. ## Enums follow the same rule For an enum, every field of **every variant** must be `Copy`. A C-like enum such as `enum Layer { Background, World, Ui }` derives `Clone, Copy` without trouble, and so does an enum whose payloads are all numbers. One owning payload is enough to block it: deriving `Copy` on `enum Event { Tick(u32), Say(String) }` fails with E0204, and the error marks the `String` inside the `Say` variant. Adding such a variant later therefore removes `Copy` from the whole enum, which is one more reason the `Copy` decision is part of a type's public contract. ## What the derive generates for generics On a generic type, `#[derive(Clone, Copy)]` adds a bound on every type parameter: ```rust #[derive(Clone, Copy)] struct Tagged<T> { id: u32, tag: T } // generates, in effect: // impl<T: Copy> Copy for Tagged<T> {} // impl<T: Clone> Clone for Tagged<T> { ... } ``` So the definition always compiles, and `Copy`-ness is decided per use: `Tagged<char>` is `Copy`, `Tagged<String>` is only `Clone`, and passing it to a function bounded by `T: Copy` fails with E0277. The bound can be stricter than needed: a struct holding `fn() -> T` or `PhantomData<T>` never stores a `T` by value, yet the derive still demands `T: Copy`. The standard library's documentation shows the fix for that case: write the two impls by hand, with `clone` returning `*self`. ## Should your type be Copy? The `Copy` documentation says that if a type *can* implement `Copy`, it generally should, and adds a caution: implementing it "is part of the public API of your type". Removing `Copy` later breaks every caller that relied on implicit copies. Good candidates are small value types that will stay plain data: - positions, sizes and colours in a game (`Vec2`, `Rect`, `Rgba`) - numeric identifiers and handles that own nothing - C-like enums and small flag sets Types that hold a `String`, a `Vec` or another owning field are `Clone`-only by necessity, and types that might grow such a field later are safer left without `Copy`.

  • Why does `#[derive(Copy)]` without `Clone` fail?
    `Copy` is declared as `pub trait Copy: Clone`, so every `Copy` type must also be `Clone`. Deriving only `Copy` leaves that bound unmet and rustc reports E0277, "the trait bound `Vec2: Clone` is not satisfied", suggesting `#[derive(Clone)]`. The usual spelling is `#[derive(Clone, Copy)]`.
  • Why may a struct holding `&'a String` be `Copy` when `String` is not?
    Copying the struct copies only the shared reference, and `&T` is `Copy` for every `T`. No ownership of the `String` is duplicated, and the borrow checker keeps every copy from outliving the `String`. A `&'a mut String` field would make the derive fail with E0204, because `&mut T` is never `Copy`.
  • When is `#[derive(Clone, Copy)]` on a generic struct too strict?
    The derive adds `T: Copy` and `T: Clone` bounds even when the struct never stores a `T` by value, for example `struct Generate<T>(fn() -> T)` or a `PhantomData<T>` marker. Writing `impl<T> Copy for Generate<T> {}` and a `Clone` impl returning `*self` by hand removes the needless bounds.

  • A struct with a Vec field can be Copy if you derive both traits
  • A mutable reference is Copy because it is just a pointer
  • Deriving Copy alone is enough; Clone is optional
  • Only primitive types can be Copy, never user-defined structs
  • derive(Copy) on a generic struct makes it Copy for every type argument