In the Rust programming language, the level of access (visibility) to the methods and fields of structures is regulated by modifiers: pub, pub(crate), pub(super), as well as the absence of a modifier (by default - private).
Rust was initially designed to ensure reliability and isolation of components. Access control to the internal parts of structures allows encapsulating data and hiding implementation details, keeping only the necessary interfaces public.
Developers often face situations where a structure is public, but its fields remain private, or the visibility of a field is not sufficient due to visibility constraints of the structure or module itself. It is particularly difficult to understand nested levels: a public nested structure may be inaccessible if the containing module is hidden, and vice versa.
In Rust, visibility modifiers are applied to structures, their fields and methods, as well as to modules and functions. The following levels exist:
pub — makes the element accessible from anywhere.pub(crate) — accessible only within the current crate.pub(super) — accessible only from the parent module.Example code:
mod outer { pub struct PublicStruct { pub field: u32, hidden: u32, } pub(crate) struct CrateStruct { pub value: i32, } struct PrivateStruct { pub secret: i32, } pub mod inner { pub(super) struct SuperStruct { pub super_field: u8, } } }
Key features:
If a structure is declared as pub, but its fields are without a modifier, can they be accessed from another module?
No, only the structure itself becomes public, but its fields remain private within the module. For access to the field, it must also be declared as pub.
What happens if a structure is declared as pub(crate), and a field inside it is pub?
The visibility is limited to the structure itself. Even if the field is pub, it cannot be accessed outside the crate, as the structure is not accessible.
pub(crate) struct Secret { pub data: i32, // pub does not "pass through" pub(crate) }
Can a structure be declared as pub inside a private module and accessed from the outside?
No. The final visibility is determined by the minimum of the structure and the module. If the module is private, the structures and functions inside it are also not visible outside this module.
In a project, the entire structure was made public with open fields to speed up development. Later, it became difficult to maintain backward compatibility and control access to fields, as they were modified directly.
Pros:
Cons:
For a public structure, private fields and public constructor/accessor methods are implemented. The structure is exported only at the necessary module level.
Pros:
Cons: