Sitemap

Organizing Rust Code: Modules, Crates & Project StructurešŸ¦€

3 min readAug 5, 2025

--

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

What Is Rust? Learn Rust Programming with This Clear, Concise Tutorial— YouTube

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_rust

This 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.rs wouldn’t be able to call say_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.rs

Update 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; in main.rs loads the utils folder.
  • mod math; inside utils/mod.rs exposes the math module.
  • The add() function is marked pub, 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 mod to declare modules based on filenames or folders
  • Use pub to 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.rs as the module entry point for folders — just like index.js or __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→

Press enter or click to view image in full size

--

--

Ali Aslam
Ali Aslam

Written by Ali Aslam

Avid Learner | Principal Software Engineer | Full Stack Developer