How to Use pub to Control Visibility in Rust

Use the `pub` keyword in Rust to make functions, structs, or fields accessible outside their defining module.

Use the pub keyword before an item to make it accessible outside its current module. By default, items in Rust are private, so adding pub explicitly exposes functions, structs, or modules to the rest of the crate or external crates.

pub fn public_function() {
    println!("This is visible everywhere");
}

struct PrivateStruct {
    value: i32,
}

pub struct PublicStruct {
    pub value: i32,
}

The pub keyword can also be applied to struct fields individually to control granular visibility.