🦀 Rust Intermediate

What are derive macros and which common ones does Rust provide?

Answer

Derive macros (using #[derive(...)]) automatically generate trait implementations for a type based on its structure, eliminating manual boilerplate. The Rust standard library provides these commonly-derived traits: Debug (enables {:?} formatting for debugging), Clone (enables .clone() for deep copies), Copy (marks types that are bit-copyable — requires Clone), PartialEq and Eq (enables == and != comparisons), PartialOrd and Ord (enables ordering), Hash (enables use as HashMap key). Third-party derives are ubiquitous: serde::Serialize/Deserialize (JSON, etc.), thiserror::Error, and clap::Parser for CLI arguments.