← All notes

Derivable traits: the derive attribute for Debug, Clone, and Copy

Answer: The #[derive] attribute asks the compiler to provide a basic implementation of some traits. Debug, Clone, and Copy are among the traits you can use with derive. Display cannot be derived, because it has no sensible default behavior.

What derive generates

The book's derive attribute can sit on a struct or enum definition.

That attribute generates the code for a default trait implementation on the annotated type.

Appendix C lists the standard-library traits you can use with derive.

Other traits in the standard library don't have sensible default behavior. Implement them in the way that makes sense.

Display, which handles formatting for end users, is an example of a trait that can't be derived.

Libraries may add derive for their own traits, so the list is not comprehensive.

Debug formatting in format strings uses :? inside {} placeholders. assert_eq! requires Debug.

Traits the compiler can fill in

Via the #[derive] attribute, the compiler can provide basic implementations for some traits.

A manual implementation remains available when more complex behavior is required.

Comparison traits that can be derived are Eq, PartialEq, Ord, and PartialOrd.

Clone is used to create T from &T via a copy.

Copy gives a type copy semantics instead of move semantics.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Appendix C: Derivable Traits; Rust by Example, Derive. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Appendix C: Derivable Traits; Rust by Example, Derive. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:56f7rxfx

iinventedlight. 2026. "Derivable traits: the derive attribute for Debug, Clone, and Copy." iinventedlight Library, iil:note:56f7rxfx, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-derivable-traits/. License: CC-BY-4.0.

@misc{iil_note_56f7rxfx,
  author = {{iinventedlight}},
  title = {{Derivable traits: the derive attribute for Debug, Clone, and Copy}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/rust-derivable-traits/},
  note = {Permanent ID iil:note:56f7rxfx. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-derivable-traits.md.