How does the module system work

The Rust module system uses mod, pub, and use to organize code into namespaces and control visibility.

The Rust module system organizes code into hierarchical namespaces using mod declarations and controls visibility with pub and use statements. You define a module to group related functionality, mark items as public to expose them, and use use to bring them into scope.

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

use calculator::add;

fn main() {
    let result = add(2, 3);
}