keyword

Rust pub: control item visibility

Use `pub` to expose an item beyond its default private visibility. Access still depends on its enclosing modules and any visibility restrictions. `pub(crate)` limits exposure to the current crate. Expected output (not executed): 8 Why: The public function can be called from main and internally uses the private helper. Common mistake: Making an item public does not automatically make all its fields or ancestors public.

Formal Syntax & Structure

rust syntaxrust
pub item; pub(crate) item

Working Example

Example Snippetrust
mod cart {    fn unit_price() -> u32 { 4 }    pub fn total() -> u32 { unit_price() * 2 }}
fn main() {    println!("{}", cart::total());}
Rust Reference