Start with the idea
#[derive(Debug, Clone, PartialEq)] generates the mechanical implementation for each field. It only works when every field also implements the trait, which turns a missing capability deep in a struct into a clear compile error naming the field.
A trait describes a capability. Debug supports development formatting with {:?}; Clone makes an explicit copy; PartialEq enables equality comparisons; Default provides a conventional initial value. #[derive(...)] asks the compiler to generate straightforward implementations for a struct or enum. The generated code follows the fields: if a field cannot be cloned or printed, the containing type cannot derive that capability automatically.
Derive only the traits a type needs. Cloning a large structure can be expensive and may hide a design that should borrow instead. A default value is useful when it is meaningful, but Config::default() is a poor substitute for required settings that must be supplied by the caller. For an enum, deriving Default needs one designated default variant.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.