8.5 Function Scope and Nested Functions
Rust supports defining functions both at the top level of a module (similar to C) and nested within other functions.
8.5.1 Scope of Top-Level Functions
Functions defined directly within a module (not inside another function or block) are called top-level functions. They are visible throughout the entire module in which they are defined, regardless of the order of definition.
To make a top-level function accessible from other modules, you must mark it with the pub keyword (for public).
mod utils {
// This function is private to the 'utils' module by default.
fn helper() {
println!("Private helper function.");
}
// This function is public and can be called from outside 'utils'.
pub fn perform_task() {
println!("Performing public task...");
helper(); // Can call private functions within the same module.
}
}
fn main() {
utils::perform_task(); // OK: perform_task is public.
// utils::helper(); // Error: helper is private.
}
8.5.2 Nested Functions
Rust allows defining functions inside the body of other functions. These are called nested functions or inner functions. A nested function is only visible and callable within the scope of the outer function where it is defined.
fn outer_function(x: i32) {
println!("Entering outer function with x = {}", x);
// Define a nested function.
fn inner_function(y: i32) {
println!(" Inner function called with y = {}", y);
// Cannot access 'x' from outer_function here.
// println!(" Cannot access x: {}", x); // Compile Error!
}
// Call the nested function.
inner_function(x * 2);
println!("Exiting outer function.");
}
fn main() {
outer_function(5);
// inner_function(10); // Error: inner_function is not in scope here.
}
Key difference from Closures: Nested functions in Rust cannot capture variables from their enclosing environment (like x in the example above). If you need a function-like construct that can access variables from its surrounding scope, you should use a closure (Chapter 12). Nested functions are simpler entities, essentially just namespaced helper functions local to another function’s implementation.