derive
macro- Popular traits used with
derive
attribute - See traits doc
derive
attribute macro auto implements traits on a struct or enumderive
attribute macro generates code- To override the behavior, implement the trait manually expand the macro, if needed
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 |