8.4 Function Scope and Nested Functions

8.4.1 Scope of Top-Level Functions

Functions defined at the module level are visible throughout the same module. You can place them in any order; the compiler resolves them automatically.

8.4.2 Nested Functions

Nested functions (also called inner functions) are defined inside another function and are visible only within that enclosing function:

fn main() {
    outer_function();
    // inner_function(); // Error! Not in scope
}

fn outer_function() {
    fn inner_function() {
        println!("This is the inner function.");
    }

    inner_function(); // Works here
}
  • inner_function is only accessible within outer_function.