4 Making fields public with attribute macros
This chapter covers
- Understanding the differences between derive macros and attribute macros
- Finding field information in the abstract syntax tree
- Retrieving fields by using matching
- Retrieving fields with a custom struct
- Retrieving fields with a custom struct and a
Parse
implementation - Adding multiple outputs in
quote
- Debugging macros with log statements
- Understanding the
no-panic
crate
Rust likes to hide information. A function, struct, or enum is private by default, and the same goes for the fields of a struct. This is very sensible, though occasionally slightly annoying when you have a struct that has a lot of fields that are better off being public. Data Transfer Objects (DTOs) are a classic example of this and a common pattern in many programming languages, used for transferring information between systems or different parts of a single system. Because they are a simple wrapper for information, they should not contain any business logic. And “information hiding,” a primary reason for keeping fields in a struct/class private, is not applicable when your only value is exposing the information contained in fields.