If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.
7.6.1 Moving Module Contents to Another File
If the module name is followed by ; instead of a code block when defining a module, Rust will look for a .rs file with the same name as the module under the src directory and load its contents. Whether the module’s contents are in the same file or in different files, the structure of the module tree does not change.
Take a look at an example:
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() { }
}
}
pub use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
add_to_waitlist();
}
This way, all modules are placed in the same file. If you want to move them into different files, do this:
Step 1: Create a New File
If you want to split out front_of_house, you need to create a .rs file with the same name under the src directory:
Step 2: Cut the Code
Cut the code that was originally under front_of_house from its original location into the front_of_house.rs file, that is, cut out this part:
pub mod hosting {
pub fn add_to_waitlist() { }
}
Step 3: Modify the Original Location
Open the original place where front_of_house was defined. At this point, you no longer need the code block after it, so delete it together with the {} and add a ; instead (do not touch other unrelated code). The original code is:
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() { }
}
}
pub use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
add_to_waitlist();
}
Change it to:
mod front_of_house;
pub use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
add_to_waitlist();
}
7.6.2 Splitting Submodules
What if you have many modules under front_of_house? Then you will need to put these submodules into different files to better organize code. But how? Put all submodules in the folder src like what we just did? Then src contains too many files and the hierarchical relationship between the modules cannot be displayed.
Rust gives a pretty good solution for it: put all submodule files in a folder named by their father module. Specifically, you need to create a folder with the same name as the parent module first, and then use a .rs file inside that folder to store the submodule or items.
For example, if I want to split out hosting as a separate file, I do not just create a .rs file with the same name in src. I first need to create a folder with the same name as the parent module. In this example, the parent module is named front_of_house, so I need to create a folder named front_of_house.
Then create a .rs file in that folder with the same name as the item or module. In this example, since I want to split out hosting, the file should be named hosting.rs.
Store the contents of hosting in hosting.rs, which is:
pub fn add_to_waitlist() { }
Now you can delete the code block of hosting module in front_of_house.rs together with the {} and add a ;, same process as what we did to lib.rs. Change it from (front_of_house.rs):
pub mod hosting {
pub fn add_to_waitlist() { }
}
to simply:
pub mod hosting;
Rust also support split modules in the form ofmodule_name/mod.rs. All modules are stored in mod.rs. Folder names imply module names. This method is still fully supported in Rust, but in modern Rust code, it is usually more like a continuation of the old-style module layout rather than the default preference.
If we use this method to split modules, it will look like:
7.6.3 Benefits of Splitting
As modules grow larger, this technique lets programmers move a module’s contents into other files.






Top comments (0)