Use visibility to decide which modules and crates can access a declaration. Rust declarations are private by default: a private item is accessible within its module and submodules. Add `pub` only where access is needed. section 2.9
Choose the narrowest scope that serves the caller:
- `pub` makes an item public; `pub(crate)` permits access within the crate but not from another crate. section 2.9
- `pub(self)`, `pub(super)`, and `pub(in path)` restrict access to the named module scope. section 2.9
- Visibility applies separately to a struct and each of its fields. Making a struct public does not make its fields public. section 4.5.3
mod private_mod {
pub fn private_mod_fn() {}
}
pub mod public_mod {
pub fn public_mod_fn() {}
}
Both functions are marked `pub`, but the function inside `private_mod` is not exported through that private module. A public item also needs a publicly accessible module path. section 2.9
pub struct MixedVisibilityStruct { #1
pub name: String, #2
pub(crate) value: String, #3
pub(super) number: i32, #4
}
The public struct gives its fields different access scopes: `name` is public, `value` is crate-visible, and `number` is visible from the parent module. section 4.5.3
Keep fields private unless callers need direct access or modification. Also avoid exporting unnecessary names: modules help contain namespace pollution. One default-privacy exception is that associated items of public traits and public enums are public by default. section 4.5.3 section 2.9







