How to Use #[derive] in Rust
#[derive] asks the compiler to write standard trait implementations for your struct. With Debug, Clone and PartialEq derived, a struct can be printed with {:?}, copied with .clone() and compared with ==, without writing those methods by hand.
Syntax
#[derive(<Trait1>, <Trait2>, <Trait3>)]
struct <Name> {
<field>: <Type>,
}
| Part | What it does |
|---|---|
#[derive(…)] | An attribute on the line above the struct. It generates an implementation of every trait it lists. |
Debug | Lets {:?} and {:#?} print the value, for programmers. |
Clone | Adds .clone(), which makes an independent copy. |
PartialEq | Lets == and != compare two values. |
The Rust Reference on derive: "The derive attribute invokes one or more derive macros, allowing new items to be automatically generated for data structures." It "may only be applied to structs, enums, and unions."
The Example
The example derives all three traits on a Point with x and y, prints it, clones it, and checks that the copy equals the original.
Prerequisites
- Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.0.
Step 1: Create a Project
cargo new demo
cd demo
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
cargo new demo creates a folder named demo with a Cargo.toml file, which describes the project, and src/main.rs, which holds the code. cd demo moves into it.
Step 2: Write the Code
Replace the contents of src/main.rs with:

#[derive(Debug, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let a = Point { x: 1, y: 2 };
let b = a.clone();
println!("{:?}", a);
println!("{}", a == b);
}
a.clone() makes b from a, {:?} prints a with its Debug implementation, and a == b uses the PartialEq one.
Step 3: Run It
cargo run -q
Point { x: 1, y: 2 }
true

cargo run compiles the program and runs it, and -q hides Cargo's own status lines. Debug printed the struct's name and every field, and == returned true because the copy has the same values. The Rust Book's appendix on derivable traits explains the rule: "When PartialEq is derived on structs, two instances are equal only if all fields are equal".
Print It on Several Lines
{:#?} is the pretty form of {:?}, with one field per line:
Point {
x: 1,
y: 2,
}
What Each Missing Trait Looks Like
Each trait you leave out of #[derive] gives its own compile error, and each error suggests the fix:
| Left out | Error | What the compiler suggests |
|---|---|---|
Debug | error[E0277]: `Point` doesn't implement `Debug` | consider annotating `Point` with `#[derive(Debug)]` |
PartialEq | error[E0369]: binary operation `==` cannot be applied to type `Point` | consider annotating `Point` with `#[derive(PartialEq)]` |
Clone | error[E0599]: no method named `clone` found for struct `Point` in the current scope | the following trait defines an item `clone`, perhaps you need to implement it |
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Deriving Display | error: cannot find derive macro `Display` in this scope, with the note `Display` is a trait, not a derive macro | Implement Display by hand, or print with {:?} |
Deriving only Debug and never reading the fields | warning: fields `x` and `y` are never read: the compiler notes that the derived Debug is "intentionally ignored during dead code analysis" | Use the fields, or leave the warning while you build |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
doesn't implement `Debug` | Debug isn't derived | Add Debug to #[derive(…)] |
binary operation `==` cannot be applied | PartialEq isn't derived | Add PartialEq |
no method named `clone` found | Clone isn't derived | Add Clone |
FAQ
Which traits can be derived?
From the standard library: Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash and Default. The Rust Book's appendix covers each one. Crates add their own, such as serde's Serialize and Deserialize.
What does a derived clone do?
The Book's appendix says that deriving Clone "implements the clone method, which when implemented for the whole type, calls clone on each of the parts of the type."
Conclusion
#[derive(Debug, Clone, PartialEq)] above a struct gives it printing with {:?}, copying with .clone() and comparing with ==. When the compiler says a trait is missing, its help line names the derive to add.
Additional Resources
- Derive, in the Rust Reference.
- Appendix C: Derivable Traits, in the Rust Book.
