Organizing Rust Code: Modules, Crates & Project Structureš¦
Part of series: Rust Tutorial for Beginners: Learn the Rust Programming Language from Scratch and Build a WebAssembly Ready App (Article Series)
If youāve been learning Rust, you might have started by writing everything inside a single main.rs file. While this is convenient for small examples, real-world projects quickly outgrow this approach. Organizing your code into multiple files and modules helps keep your project clean, maintainable, and scalable.
You can switch to video version of Rust Crash Course to the relevant chapter
In this article, weāll explore how Rust organizes code across files and modules, focusing on key concepts like mod, use, pub, and directory structure.
Why Modules Matter in Rust
At first, putting everything inside main.rs might seem fine ā but as your project grows, it becomes harder to navigate and manage.
Rustās module system lets you split your code into separate files and folders, making it easier to reason about and collaborate on.
Think of modules as logical āsub-filesā or ānamespacesā that help group related functions and types.
ā Step 1: Create a New Project
Letās start fresh with a new Rust project:
cargo new modular_rust
cd modular_rustThis gives you the usual layout, with your entry point in src/main.rs.
š Step 2: Add a Module to main.rs
Open src/main.rs and declare a module:
mod greetings;
fn main() {
greetings::say_hello();
}This line:
mod greetings;tells Rust to look for a file named greetings.rs inside the src directory. That file will contain a module called greetings.
āļø Step 3: Create src/greetings.rs
Now create greetings.rs in the same src folder:
pub fn say_hello() {
println!("š Hello from a separate module!");
}A couple of important notes here:
- The function is marked
pub, which means public. In Rust, everything is private by default. - Without
pub,main.rswouldnāt be able to callsay_hello()from this module.
š ļø Step 4: Nesting Modules in Folders
Now, letās take this a step further and group related code inside a folder structure.
Get Ali Aslamās stories inĀ yourĀ inbox
Join Medium for free to get updates fromĀ thisĀ writer.
Suppose we want a utils folder for reusable functionality.
mkdir src/utils
touch src/utils/mod.rsUpdate main.rs to include both greetings and utils:
mod greetings;
mod utils;
fn main() {
greetings::say_hello();
utils::math::add(5, 7);
}Inside src/utils/mod.rs, declare a nested module:
pub mod math;Then create src/utils/math.rs:
pub fn add(a: i32, b: i32) {
println!("{} + {} = {}", a, b, a + b);
}Now when you run the program, it outputs:
š Hello from a separate module!
5 + 7 = 12š Whatās Happening Here?
This setup introduces a nested module: utils::math::add.
Letās break it down:
mod utils;inmain.rsloads theutilsfolder.mod math;insideutils/mod.rsexposes themathmodule.- The
add()function is markedpub, so it can be accessed externally. - We call it in
main()using the full path:utils::math::add(5, 7);
This folder-based module pattern is idiomatic Rust.
š” If You Know Other Languagesā¦
Rustās mod.rs inside a folder works a lot like:
⢠index.js in a JavaScript module ⢠__init__.py in a Python package
It signals: āThis folder is a module,ā and acts as its entry point. From there, you can re-export internal modules like math.rs.
š§ Summary
Hereās what youāve learned:
- Use
modto declare modules based on filenames or folders - Use
pubto expose functions across files or modules - Rustās folder and file structure maps directly to its module system
- Use nested modules to organize related functionality as your project grows
- Think of
mod.rsas the module entry point for folders ā just likeindex.jsor__init__.py
Final Thoughts
With this foundation, your Rust projects will be much easier to navigate, test, and scale. Youāll also find that working in a modular style leads to more reusable, composable, and idiomatic Rust code.
Next time you start coding in Rust, try to organize your code into modules from the beginning. It will pay off as your project grows!
Next up: ā”ļøMini Project ā Build a Simple WebAssembly-Ready App in Rustā






