Dividing Code Into Modules
Learn how to divide our code into modules using single-file modules.
We'll cover the following...
We'll cover the following...
We can write huge programs in one file, but dividing our code into smaller sections—known as modules in Rust—offers significant advantages:
- It’s much easier to find our map functionality in a file named
map.rs
than it is to remember that it’s somewhere around line 500 of an ever-growingmain.rs
. - Cargo can compile modules concurrently, producing much better compilation times.
- Bugs are easier to find in self-contained code that limits linkages to other sections of code.
Modules may either be a single .rs
file or a directory.
Crates and modules
Rust programs are divided into crates and modules. Crates are large groups of code with their own Cargo.toml
file.
Our game is a crate. So is bracket-lib; every dependency we specify in Cargo is a ...