Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 2.85 KB

traits.derive.md

File metadata and controls

37 lines (29 loc) · 2.85 KB

Overview

  1. derive macro
  2. Popular traits used with derive attribute
  3. See traits doc

Key Concepts

  1. derive attribute macro auto implements traits on a struct or enum
  2. derive attribute macro generates code
  3. To override the behavior, implement the trait manually expand the macro, if needed

Traits

Trait Purpose Compiler usage Requirements
Clone Explicit copy using .clone(), make T from &T N/A TODO
Copy Rarely useful Use copy semantics for a type TODO
Debug For Programmers, Enable formatting using {:?} assert_eq! TODO
Default Create empty instance with reasonable default fields TODO
Eq PartialEq
Error TODO
Hash PartialEq and Eq
Ord Can compare and order All items of the type Eq
PartialEq compare any x and y, but x != x for some values, like f64 ==
PartialOrd comparisons <, <=, >, >= PartialEq
Serialize convert struct or enum to string/bytes N/A
Deserialize convert bytes/string to struct or enum N/A

Notable exceptions

  1. Display

Other Resources

  1. https://doc.rust-lang.org/rust-by-example/trait/derive.html
  2. https://www.lurklurk.org/effective-rust/std-traits.html
  3. https://dev.to/werner/move-semantics-vs-copy-semantics-pkb