9.10 Visibility and Modules
Rust carefully manages visibility. By default, structs and fields are private to the module in which they’re defined. Making them accessible outside their module requires using the pub keyword.
9.10.1 Visibility with pub
pub struct PublicStruct {
pub field: Type,
private_field: Type,
}
PublicStructis visible outside its defining module.fieldis publicly accessible, butprivate_fieldremains private.
9.10.2 Modules and Struct Visibility
By default, structs and fields are private within their module, meaning they cannot be accessed externally. This design promotes well-defined APIs and prevents external code from relying on internal implementation details. You will learn more about modules and crates later in this book.