Fixing Rust Error E0583: File Not Found for Module

beginnerπŸ¦€ Rust2026-07-15| Rust (all versions), Cargo, Linux, macOS, Windows

Error Message

error[E0583]: file not found for module `my_module` help: to create the module `my_module`, create file "src/my_module.rs" or "src/my_module/mod.rs"
#rust#modules#project-structure#compiler-error

The Problem

Rust handles modules differently than languages like Python or JavaScript. In Rust, the mod keyword doesn't just import code; it declares that a module exists. When you write mod auth; in your main.rs, you are telling the compiler to go find a file that contains the code for the auth module. If the compiler searches the expected directories and comes up empty-handed, it triggers error[E0583].

Think of this error as a missed connection. You've announced that a new part of your program exists, but you haven't given the compiler the physical file to back up that claim. This usually happens when moving code out of a 200-line main.rs into a dedicated file for the first time.

error[E0583]: file not found for module `my_module` 
help: to create the module `my_module`, create file "src/my_module.rs" or "src/my_module/mod.rs"

Quick Solutions

The compiler output actually provides a roadmap for the fix. It suggests two specific file paths. Depending on how you want to organize your code, choose one of the two structures below.

1. The Flat Approach (Recommended for most cases)

Modern Rust (Edition 2018 and later) prefers keeping modules as standalone files in the src/ directory. If your module is called auth, simply create auth.rs.

Project Structure:

my_project/
β”œβ”€β”€ Cargo.toml
└── src/
    β”œβ”€β”€ main.rs
    └── auth.rs  <-- Create this file

Inside main.rs, your declaration remains simple:

mod auth; // Rust automatically looks for src/auth.rs

2. The Folder Approach (For complex modules)

Sometimes a module grows too large for a single file. If your auth module needs its own sub-modules (like auth::saml and auth::oauth), use a folder. Create a directory named auth and put a mod.rs file inside it.

Project Structure:

my_project/
β”œβ”€β”€ Cargo.toml
└── src/
    β”œβ”€β”€ main.rs
    └── auth/
        └── mod.rs    <-- This acts as the module's entry point

Rust treats src/auth/mod.rs as the equivalent of src/auth.rs.

Dealing with Nested Modules

Complexity increases when you nest modules. Suppose you have a database module with a specific connector: db::connector.

If you declare mod connector; inside src/db.rs, the compiler expects the file to be relative to the db module. You have two valid options for the file path:

  • Option A: src/db/connector.rs (The modern, cleaner style)
  • Option B: src/db/connector/mod.rs (The older, hierarchical style)

A common mistake is placing connector.rs directly in the src/ folder. This will fail. Rust requires sub-modules to live in a subdirectory named after their parent module.

Common Debugging Checklist

Watch for Hidden Extensions

Verify your file ends in exactly .rs. Basic text editors sometimes sneakily append .txt to the end. On Windows, a file named auth.rs.txt might appear as auth.rs if you have "Hide extensions for known file types" enabled. If you suspect this, check your file names via the terminal using ls or dir.

Respect Case Sensitivity

Rust is strictly case-sensitive. If your code says mod Database; but your file is named database.rs, the compiler will fail on Linux and macOS. These operating systems use case-sensitive filesystems. To avoid headaches, always use snake_case for both filenames and module declarations.

Keep Declarations at the Top Level

While Rust allows you to declare modules inside functions, it's rarely a good idea. Doing so creates confusing path resolution rules. Stick to placing your mod statements at the top of your main.rs or lib.rs files.

Verifying the Fix

Don't wait for a full build to see if you've fixed the error. Run a quick check instead:

cargo check

This command analyzes your code without the overhead of generating an executable. It usually finishes in under 2 seconds for small projects. If the error persists, compare the path in the error message character-for-character with your actual file path. If the compiler says it wants src/api/routes.rs, ensure you haven't accidentally placed it in src/routes.rs.

Key Takeaways

  • mod is a declaration, not a simple import. It tells Rust to look for a file.
  • Your file system must mirror your module hierarchy perfectly.
  • Prefer the name.rs style over name/mod.rs for a cleaner project layout.
  • Always use snake_case for filenames to maintain cross-platform compatibility.

Related Error Notes