Day 8 - Chapter 7: Managing Growing Projects with Packages, Crates, and Modules
1. Introduction
Today, we’re going to cover one of the most important parts of learning Rust - how to organize your code as your project grows.
When you first start writing Rust, everything might fit nicely in a single main.rs file. But as your program gets bigger, things can quickly get messy. That’s where packages, crates, and modules come in.
2. Packages and Crates
Packages are the containers that hold your Rust projects.
A package can contain:
- One or more crates (projects or libraries).
- A Cargo.toml file (this is like the project’s recipe - it lists dependencies and metadata).
There are two main types of crates:
- Binary crate → makes an executable program (like cargo run).
- Library crate → provides reusable code (used by other crates).
3. Defining Modules to Control Scope and Privacy
When your Rust project grows, you’ll start having many functions, structs, and enums.
To keep everything neat and avoid name clashes, Rust lets you group related code into modules using the mod keyword.
Think of a module as a folder or section of your project - a place to group code that belongs together.
Example: A Restaurant Analogy
Let’s say we’re building a restaurant app. We could have different parts (modules) for the restaurant’s sections, like the front of the house and the back of the house.
The struct would look something like this :
#![allow(unused)] fn main() { mod front_of_house { pub mod hosting { pub fn add_to_waitlist() { println!("Added to waitlist!"); } fn seat_at_table() { println!("Seating the customer..."); } } mod serving { fn take_order() {} fn serve_order() {} fn take_payment() {} } } }
Breaking This Down
mod front_of_house - defines a module named front_of_house.
Inside it, we have two submodules:
-
pub mod hosting -
mod serving
The pub keyword means public - it tells Rust that other parts of the program can use this module or function.If we don’t mark something as pub, Rust keeps it private by default, meaning it can only be used inside the same module.
So in our example:
add_to_waitlist() ✅ is public - other modules can call it.
seat_at_table() ❌ is private - only hosting can use it.
Using the Module from Another Place
If you want to use add_to_waitlist() from outside (like from main.rs), you’d write:
fn main() {
crate::front_of_house::hosting::add_to_waitlist();
}
Here’s what’s happening:
Cratemeans “start at the root of my current project.”- Then we go down the tree:
front_of_househostingadd_to_waitlist()
4. Paths for Referring to an Item in the Module Tree
To use a function or struct inside a module, you need to refer to it by its path - kind of like how you use file paths in a computer.
There are two types of paths:
-
Absolute path → starts from the root of your crate.
-
Relative path → starts from where you currently are in the module tree.
💡 Example:
crate::front_of_house::hosting::add_to_waitlist(); // absolute
front_of_house::hosting::add_to_waitlist(); // relative
Think of it like finding a file:
/home/user/docs/file.txt → absolute
../docs/file.txt → relative
5. Bringing Paths Into Scope with the use Keyword
The use keyword is a shortcut to make long paths shorter and cleaner.
Instead of writing:
crate::front_of_house::hosting::add_to_waitlist();
You can bring the path into scope:
use crate::front_of_house::hosting;
hosting::add_to_waitlist();
It’s like creating a desktop shortcut so you don’t have to dig through folders every time you want to open a file.
6. Separating Modules into Different Files
As your code grows, you can split modules into different files to stay organized.
Example project structure:
src/
├── main.rs
├── front_of_house.rs
└── front_of_house/
└── hosting.rs
In your main.rs or lib.rs, you declare:
mod front_of_house;
Rust automatically looks for:
front_of_house.rs, or
A folder named front_of_house containing a mod.rs or nested modules.
This keeps your codebase modular and easy to navigate - just like storing things in labeled boxes instead of one giant drawer.
7. Summary
| Concept | Think of it like… | What it does |
|---|---|---|
| Package | A full project | Groups crates, has Cargo.toml |
| Crate | A house | A binary or library of Rust code |
| Module | A room in the house | Organizes related code |
| Path | The address | Shows how to reach an item |
use keyword | Shortcut | Brings names into scope |
| Separate files | Different rooms | Keeps your code tidy |