Module Declarations in a Conventional Project Layout
How to connect your Axum project's source files using Rust mod declarations so the compiler can find and build every module
When you split a Rust project into multiple files and folders, the compiler does not automatically look at every .rs file on disk. You have to explicitly tell it which files belong to the project. That is exactly what a mod declaration does: it connects a file or folder on your disk to the module tree that the Rust compiler builds in memory.
If you have followed the conventional layout so far, you likely have a main.rs, a lib.rs, and empty directories such as routes/, handlers/, models/, plus placeholder files state.rs and error.rs. None of those files are part of the program yet. Rust will happily ignore them until you write a mod statement for each one in the crate root - which, for a library crate like ours, is lib.rs.
This page explains exactly how to write those declarations, where to place them, and what can go wrong when you miss one.
Why Mod Declarations Exist
Rust treats every .rs file as a module, but it does not scan your folder structure automatically. The reason is control: you might have helper files, notes, or experimental code that should not be compiled. By requiring explicit declarations, Rust lets you choose exactly which files become part of your program.
In a single-file project, everything lives inside main.rs and there is nothing to declare. As soon as you create routes/ or handlers/, that changes. The compiler needs a bridge from main.rs or lib.rs to those files. The mod keyword is that bridge.
Think of the module tree as an org chart. The crate root (lib.rs) is the boss. The mod statements are the boss pointing at each department and saying "this department exists." Without that pointing, the department is not on the chart at all.
The Crate Root and the Module Tree
In the project structure this page works with, we have both main.rs and lib.rs. main.rs is the binary entry point. lib.rs defines a library crate that main.rs can import. All the modules we create - routes, handlers, models, state, error - belong to the library crate, so their mod declarations go in lib.rs.
That means main.rs does not directly declare these modules. Instead, it uses the library crate as a whole. We will see how that works later in this page. For now, remember: in a split entry point project, module declarations live in lib.rs.
If your project only had main.rs and no lib.rs, you would put the mod statements in main.rs. But our convention uses a library crate, so lib.rs is the root.
A First Minimal Example
Let's start with the smallest possible module declaration. Imagine you have this file structure:
src/
lib.rs
main.rs
models.rs
Inside models.rs you have a simple placeholder:
// src/models.rs
pub fn placeholder() {
println!("models module loaded");
}
For Rust to compile that file, you must write this in lib.rs:
// src/lib.rs
mod models;
That one line tells the compiler: "There is a module named models, and its source code lives in a file called models.rs in the same directory as this file." The compiler now reads models.rs and includes it in the crate.
Notice the file name matches the module name exactly. mod models; looks for models.rs (or models/mod.rs if a directory is involved). That matching is required.
The Conventional Layout with Directories
In a conventional Axum layout, most modules are directories containing their own files. For example:
src/
lib.rs
main.rs
routes/
mod.rs
handlers/
mod.rs
models/
mod.rs
state.rs
error.rs
Each directory has a mod.rs file. That file acts as the entry point for that module, just like lib.rs is the entry point for the whole crate. Inside routes/mod.rs, you can later declare submodules or re-export items, but for now we just need it to exist.
The corresponding declarations in lib.rs are:
// src/lib.rs
pub mod routes;
pub mod handlers;
pub mod models;
pub mod state;
pub mod error;
When you write mod routes;, Rust looks for either routes.rs or routes/mod.rs. Since we have a directory, it finds routes/mod.rs. Same for handlers and models. The files state.rs and error.rs are plain files, so they are found directly.
The declarations use pub because main.rs is a separate crate that imports from the library, and only public modules are visible to it. The next sections explain exactly why that pub matters and what error you get when it is missing.
All five modules are now part of the library crate. Even if those mod.rs files are empty, the compiler is happy because the modules exist.
A Complete Working Example
Let's make each placeholder module contain something tiny so we can verify the whole project compiles and runs. We will keep the content deliberately simple - no Axum routes yet, just enough to prove the module system works.
First, the file routes/mod.rs:
// src/routes/mod.rs
pub fn placeholder() {
println!("routes module is connected");
}
Then handlers/mod.rs:
// src/handlers/mod.rs
pub fn placeholder() {
println!("handlers module is connected");
}
Then models/mod.rs:
// src/models/mod.rs
pub fn placeholder() {
println!("models module is connected");
}
Then state.rs:
// src/state.rs
pub fn placeholder() {
println!("state module is connected");
}
And error.rs:
// src/error.rs
pub fn placeholder() {
println!("error module is connected");
}
Now lib.rs declares all of them:
// src/lib.rs
pub mod routes;
pub mod handlers;
pub mod models;
pub mod state;
pub mod error;
At this point the library crate compiles. But if you run cargo run, nothing will happen because main.rs does not call any of these functions. Let's make main.rs use the library crate and call one placeholder to see the connection in action.
// src/main.rs
use my_project_name::routes;
fn main() {
routes::placeholder();
}
Replace my_project_name with the actual name of your crate as defined in Cargo.toml. If your crate is named axum_project, the line becomes use axum_project::routes;.
Now run cargo run. You should see:
routes module is connected
That confirms the module declaration worked: main.rs reached into the library crate, found the routes module, and called its function.
Everything is wired correctly:
If you see the output above, your module declarations are correct. The compiler has accepted every file as part of the project, and the binary can access the library's modules.
Adding pub to Make Modules Visible
Every lib.rs listing on this page so far uses pub mod, and that pub is doing real work. When you have both a library and a binary in the same package, the binary is a separate crate that depends on the library crate. It can only access items that are marked pub in the library crate.
That rule applies to the module itself, not just to the functions inside it. If you wrote mod routes; without pub, and pub fn placeholder() inside that module, main.rs still could not call routes::placeholder(). The function is public, but the module that contains it is private, so main.rs cannot see the module at all. To allow your own binary to reach a module, you must declare the module as pub mod.
If you forget the pub, the compiler stops you with this error:
error[E0603]: module `routes` is private
--> src/main.rs:1:22
|
1 | use my_project_name::routes;
| ^^^^^^ private module
That error is the compiler telling you that the module exists but is not visible outside the library crate. The fix is one word: change mod routes; to pub mod routes; in lib.rs, and do the same for every other module the binary needs.
Private modules hide everything inside:
If you forget the pub keyword on a module that your binary or another crate needs to use, the module will be invisible to them. Public functions inside a private module are still unreachable. Always think about who needs to see this module from the outside.
Mod Declarations vs Use Statements
New Rust programmers often confuse mod with use. They do different jobs.
moddeclares that a module exists and connects it to a file or folder.useimports an item from an already-declared module into the current scope so you can refer to it by a shorter name.
You can only use something after it has been declared with mod (or if it is part of the standard library or an external crate).
For example:
// lib.rs
pub mod routes;
use routes::placeholder; // now you can call placeholder() directly inside lib.rs
But use does not declare a module. If you write use routes; without a preceding mod routes;, the compiler will complain that routes could not be found.
A helpful mental model:
Think of mod as "create the container," and use as "bring something from the container into my workspace." You must create the container before you can take things out of it.
A Step-by-Step Workflow for Adding Module Declarations
If you are starting from a fresh conventional layout with empty mod.rs files, the process of wiring everything up is straightforward. Here is a typical sequence you can follow.
Step 1: Confirm the file structure
Open your src/ folder and verify that every directory has a mod.rs file and that standalone files like state.rs and error.rs exist. Without those files, a mod declaration will fail because there is no source file to compile.
src/
lib.rs
main.rs
routes/
mod.rs
handlers/
mod.rs
models/
mod.rs
state.rs
error.rs
Step 2: Open lib.rs and add declarations
For each module you want to include, write a pub mod line. The module name must match the file or directory name exactly.
// src/lib.rs
pub mod routes;
pub mod handlers;
pub mod models;
pub mod state;
pub mod error;
If you forget one, that module will not be compiled. There will be no warning - the file is simply ignored.
Step 3: Compile and check for errors
Run cargo check to verify that all modules are recognized. If you see an error about a module not found, it usually means the file name does not match the module name, or the file is in the wrong location.
cargo check
If everything is correct, you should see no errors.
Step 4: Use the modules from main.rs
In main.rs, import the library crate and call a public item from one of the modules to confirm the connection works.
// src/main.rs
use my_project_name::routes;
fn main() {
routes::placeholder();
}
Run cargo run. Seeing the placeholder output means the module tree is properly connected.
Declaring Submodules Inside a Module
Once you have pub mod routes; in lib.rs, you can later add submodules inside the routes module. For example, you might create routes/users.rs for user-related routes. To include that submodule, you would open routes/mod.rs and write:
// src/routes/mod.rs
pub mod users;
Rust will look for users.rs inside the routes/ directory, or a routes/users/mod.rs directory. This is the same pattern one level deeper.
If you do not declare users inside routes/mod.rs, the file routes/users.rs will not be compiled, even if it contains valid code. Every level of the module tree needs its own declarations.
Nested files are not automatically included:
A common mistake is to create a file like routes/users.rs and forget to add mod users; inside routes/mod.rs. The compiler will not complain about the missing file - it will simply never see it. Your route will not exist, and you will spend a long time debugging why the endpoint returns 404.
What Happens When You Forget a Mod Declaration
Let's see a concrete example of the error you get when a module is not declared. Suppose you have models/mod.rs with a public function, but lib.rs does not contain pub mod models;. Then you try to use it from main.rs:
// src/main.rs
use my_project_name::models;
fn main() {
models::placeholder();
}
When you run cargo check, you will see an error similar to:
error[E0432]: unresolved import `my_project_name::models`
--> src/main.rs:1:5
|
1 | use my_project_name::models;
| ^^^^^^^^^^^^^^^^^^^^^^^ no `models` in the root
This error does not say "file missing." It says there is no models in the crate root. That is exactly the symptom of a missing mod declaration. The fix is to add pub mod models; to lib.rs.
A missing mod declaration for a module that is never used produces no error at all. The file is silently ignored. That is why it is dangerous: you might believe your code is part of the project when it is not being compiled.
Silently ignored files:
If you create error.rs and never declare it, Rust will never compile it. Typos, logic errors, and even syntax errors inside that file will not be caught until you add the mod line. Always declare every file you intend to include.
Multiple Ways to Organize Module Files
Rust gives you two main ways to map a module name to a file path. The conventional layout uses directories with mod.rs, but you should know the alternative because you will encounter both in the wild.
Option 1: mod.rs inside a directory (classic style)
src/
lib.rs
routes/
mod.rs
In lib.rs, write pub mod routes;. Rust looks for routes.rs first, then routes/mod.rs, so either layout works - this option uses the directory form. The pub is there for the same reason as before: the binary crate needs to see the module.
Option 2: Same-name file next to a directory (Rust 2018 style)
src/
lib.rs
routes.rs
routes/
users.rs
In lib.rs, write pub mod routes;. Rust looks for routes.rs first. Inside routes.rs, you can declare submodules as mod users;, and Rust will look for users.rs inside the routes/ directory.
Both styles are valid and work identically. The classic mod.rs style is still very common in older projects and is fine to use. Many Axum tutorials and older codebases use mod.rs. Our conventional layout uses mod.rs, so we stick with that.
Which style should you choose?:
If you are starting a new project, either style is acceptable. The Rust team recommends the non-mod.rs style for new code, but the mod.rs style is not deprecated and remains widely used. Follow what your team or tutorial uses. In this project layout, we use mod.rs.
Modules Are Not Folders - They Are a Tree
It helps to stop thinking of modules as folders. The filesystem is just a storage mechanism. The module tree is a logical hierarchy inside the compiler. You can have a module named foo whose source comes from weird_path/bar/mod.rs - you would just need to use a #[path] attribute, though that is rarely necessary and we will not cover it here. For normal projects, file names and module names match, but the mental separation remains useful: mod declarations build the logical tree, and the compiler uses naming conventions to find the physical files.
When you declare pub mod routes; in lib.rs, you are not "opening the folder." You are creating a new node in the module tree and telling the compiler where to find its source. That node can later have child nodes via further mod declarations inside routes/mod.rs.
Common Beginner Mistakes and How to Avoid Them
- Forgetting
pubon modules that external code needs. Ifmain.rsor another crate must access a module, it must bepub mod, not justmod. Private modules are invisible outside the library. - Mixing up
modanduse. Usemodexactly once to declare the module, then useusewherever you need to bring items into scope. - File name mismatch.
mod routes;requiresroutes.rsorroutes/mod.rs. A file namedroute.rsorRoutes/mod.rswill not be found. Case matters. - Forgetting to declare submodules. Creating
routes/users.rswithout addingmod users;inroutes/mod.rsleaves that file uncompiled. - Declaring the same module twice. If you write
mod routes;in bothlib.rsandmain.rs, you will not get duplicate definition errors -lib.rsandmain.rsare two separate crates, so each simply compiles its own copy of the file. That is still a mistake: the two copies are different modules, so types defined in one are not the same types as in the other. In a split entry point, declare modules only inlib.rsand reach them frommain.rsthrough the library name. - Assuming the compiler scans the filesystem. Rust does not auto-discover
.rsfiles. Every file must be reachable from the crate root through a chain ofmoddeclarations.
The most consequential mistake:
The most dangerous error is silently forgetting a mod declaration. Unlike a typo, which the compiler catches, an undeclared file gives no warning. You might spend hours debugging why a route or handler doesn't work, only to find it was never compiled. Always double-check that every .rs file you create has a corresponding mod declaration somewhere in the chain.
A Full Example After Declaring All Modules
Let's put everything together with a complete project that compiles and runs. We'll keep placeholder functions simple so the focus stays on module wiring.
File: src/lib.rs
// src/lib.rs
pub mod routes;
pub mod handlers;
pub mod models;
pub mod state;
pub mod error;
File: src/routes/mod.rs
// src/routes/mod.rs
pub fn placeholder() {
println!("routes module is connected");
}
File: src/handlers/mod.rs
// src/handlers/mod.rs
pub fn placeholder() {
println!("handlers module is connected");
}
File: src/models/mod.rs
// src/models/mod.rs
pub fn placeholder() {
println!("models module is connected");
}
File: src/state.rs
// src/state.rs
pub fn placeholder() {
println!("state module is connected");
}
File: src/error.rs
// src/error.rs
pub fn placeholder() {
println!("error module is connected");
}
File: src/main.rs
// src/main.rs
use my_project_name::routes;
fn main() {
routes::placeholder();
}
Remember to replace my_project_name with the name of your crate. If you run cargo run, you will see:
routes module is connected
This confirms that lib.rs correctly declared all five modules, and the binary can reach at least the routes module. If you wanted to call the other placeholders, you could add similar use statements and calls.
How the Compiler Resolves Mod Declarations
When the compiler reads mod routes; from lib.rs, it performs a search:
- Look for a file named
routes.rsin the same directory. - If not found, look for a directory named
routes/containing a file namedmod.rs. - If neither exists, report an error: "file not found for module
routes."
This search is done at compile time. There is no runtime cost. Once the file is found, the compiler parses it and adds its contents to the module tree. Any further mod declarations inside that file are resolved using the same rules, relative to the file's location.
This mechanism is what allows a project to scale from one file to dozens of files without any special configuration. You just keep adding mod lines as your folder structure grows.
Mod Declarations and the Library Crate
Because we use a library crate (lib.rs) as the foundation of our Axum project, main.rs is just a thin wrapper. That wrapper does not need to know about the internal module structure. It only needs to import the top-level library name and whatever public items the library exposes.
That is why the module declarations live in lib.rs and not in main.rs. If you later add more modules - auth, utils, db - you will add their pub mod lines in lib.rs, and main.rs remains unchanged. This separation keeps the binary simple and the library self-contained.
A clean split:
By declaring modules only in lib.rs, you keep all structural knowledge in one place. When you return to this project months later, lib.rs acts as a map of the entire module tree.
Summary
Module declarations are the skeleton of a multi-file Rust project. They tell the compiler which files to compile and how they relate to each other. In a conventional Axum layout, you declare each top-level module in lib.rs using pub mod statements. Each module can then declare its own submodules in its mod.rs file.
The most important ideas from this page:
- Rust does not scan your filesystem. Every file must be declared with
mod. modcreates a module;useimports items from a module. They are not interchangeable.- Use
pub modwhen the module must be visible tomain.rsor any external crate. - Missing a
moddeclaration silently excludes a file from compilation - no error, no warning. - The module name must match the file or directory name exactly.
With your modules now declared, you have a valid Rust crate structure that can grow as your Axum application adds real routes, handlers, and state. The most useful next habit is to expose only the items other code actually needs from each module, keeping the public surface small on purpose. For now, cargo check should pass cleanly, and you have laid the foundation for a maintainable project.