module-concept

Rust Module trees

Organize related features under names instead of putting every function at the root. A module is a logical container, not a separate executable. Nesting establishes the paths used to reach its items. Expected result (not executed): 4 Why: The path walks through shop and shipping to the function. Common mistake: Assuming every module needs its own file.

Formal Syntax & Structure

rust syntaxrust
mod outer { mod inner {} }

Working Example

Example Snippetrust
mod shop {    pub mod shipping { pub fn fee() -> u32 { 4 } }}fn main() { println!("{}", shop::shipping::fee()); }
Rust Reference