module-concept
Rust Modules in separate files
An outlined module declaration tells Rust to locate the module source using its module-file rules. The file is compiled as part of the same crate. In this example main.rs declares price and price.rs supplies its body. A source file that is not included in the module tree does not automatically become a module. Multi-file fixture: module-file. The code shown is one source file, not a standalone program. Expected result (not executed): 18 Why: Cargo compiles the root and its declared module together. Common mistake: Adding price.rs without declaring mod price.
Formal Syntax & Structure
rust syntaxrust
mod price;Working Example
Example Snippetrust
mod price;fn main() { println!("{}", price::total()); }