How Does the Module System Work in Rust?

Rust's module system organizes code into hierarchical namespaces using `mod` and controls visibility with `pub` to manage scope and privacy.

The Rust module system organizes code into hierarchical namespaces using mod declarations and controls visibility with pub and use statements. Modules map to files or inline blocks, allowing you to group related functionality while keeping internal details private by default.

mod calculator {
    fn add(a: i32, b: i32) -> i32 { a + b }
    pub fn subtract(a: i32, b: i32) -> i32 { a - b }
}

fn main() {
    // calculator::add() is private and inaccessible here
    let result = calculator::subtract(10, 5);
}