public_fields
Makes both the struct itself and all its fields publicly accessible.
To expose a struct and its fields outside the current module:
- Annotate the struct definition with
pub
- Explicitly mark each field with
pub
modifier
This allows external code to:
- Construct instances directly using struct literal syntax
- Read/modify individual fields without accessor methods
Example
// External code can:
let p = Point ;
Note
Struct visibility also depends on parent module's visibility1,4. The containing module must be public to allow cross-module access.
For crate-internal visibility, consider pub(crate)
instead1,2.