Modules
Before we explore more about Cargo, we need to be familiar with how Rust organizes our code. We had a brief glimpse at modules in the previous chapter. Here, we will cover them in detail. Every Rust program starts with a root module. If you are creating a library, your root module is the lib.rs file. If you are creating an executable, the root module is any file with a main function, usually main.rs. When your code gets large, Rust lets you split it into modules. To provide flexibility in organizing a project, there are multiple ways to create modules.
Nested modules
The simplest way to create a module is by using the mod {} block within an existing module. Consider the following code:
// mod_within.rs mod food { struct Cake; struct Smoothie; struct Pizza; } fn main() { let eatable = Cake; }
We created an inner module named food. To create a module within an existing one, we use the mod keyword, followed by the name of the module, food, followed by a pair of braces...