Rust

E0583 File Not Found for Module Fix

This error is never about the code inside the module. The declaration `mod config;` is a promise that a file exists, and the compiler went to the exact places that promise points to — config.rs or config/mod.rs beside the file that declares it — and found nothing. Create one of those files, write the module inline instead, or tell the compiler where the file actually lives with #[path], and the same program compiles unchanged.

Written November 26, 2026 · Tested with Rust 1.99.1 · 14 min read

By the end of this guide
  • The error: a declaration with no file behind it
  • The directory form: config/mod.rs
  • The declaration moves: a mod inside a non-root file
  • No separate file at all: the inline module

Written November 26, 2026

Tested with Rust 1.99.1

What this guide is

A module declaration ending in a semicolon — mod config; — defines nothing by itself. It tells the compiler that the module's contents live in a separate file, and it obliges the compiler to go and load that file. E0583 is what it reports when the file is not where the declaration says it should be: a file wasn't found for an out-of-line module. Nothing in the message points at your logic, because the failure happens before any of the module's code is read — there is no module code yet, only the declaration and the empty place its file should occupy.

That is why every repair below changes the filesystem or the declaration, never the code that uses the module. The same failing program compiles unchanged once one of four things happens: a config.rs file appears beside the declaring file, a config/mod.rs file appears in a config directory beside it, the declaration is rewritten as an inline module with its body in braces, or a #[path] attribute points the declaration at the file that already holds the code somewhere else. The shapes differ only in which of those the project actually wants — the search itself, and the two candidate paths it checks, are the same in every case.

The error: a declaration with no file behind it

This program declares the module config and calls a function from it. No config.rs and no config/mod.rs exists next to main.rs:

Rust
mod config;

fn main() {
    println!("port = {}", config::port());
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0583]: file not found for module `config`
 --> main.rs:1:1
  |
1 | mod config;
  | ^^^^^^^^^^^
  |
  = help: to create the module `config`, create file "config.rs" or "config/mod.rs"
  = note: if there is a `mod config` elsewhere in the crate already, import it with `use crate::...` instead

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0583`.

Read the help line, because it is the whole diagnosis. The compiler names the two files it looked for — config.rs and config/mod.rs — and the span covers the entire declaration and nothing else: not the call in main, not the function name. The compiler is not saying port is missing; it never got far enough to look inside the module. The note covers the other way this error surprises people: if a mod config already exists elsewhere in the crate, this second declaration is the mistake, and the fix is a use crate::... import of the existing module rather than a new file.

The first repair is to create the file the help names first. This is the whole of the config module — one public function:

Rust
pub fn port() -> u16 {
    8080
}

Saved as config.rs beside main.rs, it is exactly what the declaration was asking for, and the original program — unchanged — now compiles and runs:

Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
port = 8080

The program prints port = 8080 and exits with status 0. The source of main.rs did not change by a single character between the failing compile and this one. What changed is that a file now exists at one of the two paths the declaration points to.

The directory form: config/mod.rs

The second candidate path puts the module's contents in a file named mod.rs inside a directory named after the module. The program is identical to the one above:

Rust
mod config;

fn main() {
    println!("port = {}", config::port());
}

This time there is no config.rs. Instead, the same one-function module body is saved as config/mod.rs — the directory config beside main.rs, holding a file named mod.rs:

Rust
pub fn port() -> u16 {
    8080
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
port = 8080

The program prints port = 8080 and exits with status 0. The two forms are equivalent to the compiler — the declaration is satisfied by either path — and the choice between them is a project-layout decision: the flat config.rs form keeps one file per module at each level, while the config/mod.rs form is the older layout, still fully supported, that some projects keep because a module with many child modules reads as a folder. What you cannot do is provide both at once; that is a different error, covered below.

The declaration moves: a mod inside a non-root file

The search is relative to the file that declares the module, not to the crate root — and this is the shape that produces E0583 in projects where the root file compiles fine on its own. Here main.rs declares util, and util.rs exists, so the first declaration is satisfied:

Rust
mod util;

fn main() {
    println!("answer = {}", util::answer());
}

But util.rs itself declares a child module, and no file for it exists in the place relative to util.rs:

Rust
mod config;

pub fn answer() -> u16 {
    config::port()
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0583]: file not found for module `config`
 --> util.rs:1:1
  |
1 | mod config;
  | ^^^^^^^^^^^
  |
  = help: to create the module `config`, create file "util/config.rs" or "util/config/mod.rs"
  = note: if there is a `mod config` elsewhere in the crate already, import it with `use crate::...` instead

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0583`.

Compare the help line with the first error's. The candidate paths have moved: the compiler now wants util/config.rs or util/config/mod.rs — a config.rs placed beside main.rs at the crate root would not satisfy this declaration, because the declaring file is util.rs and the search starts in the directory scheme that file anchors. The span says the same thing in the other direction: it points at util.rs:1:1, not at main.rs at all.

Creating the file at the first named path — util/config.rs, in a util directory beside util.rs — is the whole fix:

Rust
pub fn port() -> u16 {
    8080
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer = 8080

The program prints answer = 8080 and exits with status 0. The general rule the help line is applying: for a declaration in a file that is not itself a mod.rs or a crate root, the module's file lives in a directory named after the declaring module — util's children live under util/, just as the crate root's children live beside the root file itself.

No separate file at all: the inline module

A declaration with a body in braces never triggers a file search, because there is nothing to load. If the module is small, or the separate file was never actually wanted, rewriting the declaration as an inline module removes the obligation entirely:

Rust
mod config {
    pub fn port() -> u16 {
        8080
    }
}

fn main() {
    println!("port = {}", config::port());
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
port = 8080

The program prints port = 8080 and exits with status 0, and the module path the rest of the code uses — config::port() — is unchanged. This is the right repair when the missing file was an accident of restructuring: a module that began life as a separate file, was pasted into its parent during an edit, and left its semicolon declaration behind. It is the wrong repair as a reflex for a module that is genuinely meant to live in its own file — in that case create the file, and keep the declaration.

The file exists somewhere else: the #[path] attribute

Sometimes the module's code already exists — under a different name, or in a directory the default search never visits. Creating a second copy at the expected path would fork the code. The #[path] attribute instead tells the declaration where its file actually is. Here the module body lives in shared/settings.rs:

Rust
pub fn port() -> u16 {
    8080
}

The declaration in main.rs names that location, relative to the file the attribute sits in:

Rust
#[path = "shared/settings.rs"]
mod config;

fn main() {
    println!("port = {}", config::port());
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
port = 8080

The program prints port = 8080 and exits with status 0. The module is still called config everywhere in the code — config::port() resolves exactly as before — because the attribute changes only where the contents are loaded from, not the module's name or place in the module tree. Reach for this when a file layout is imposed from outside the crate: generated code in a build directory, a file shared between crates, or a naming scheme the default config.rs / config/mod.rs candidates cannot express. For an ordinary module in an ordinary project, creating the expected file is simpler than maintaining a path attribute on every declaration.

Neighboring case: both files present is a different error

Supplying both candidate paths does not give the compiler a choice — it gives it an ambiguity, reported under a different code. With config.rs and config/mod.rs both present beside main.rs, the same declaration fails like this:

Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0761]: file for module `config` found at both "config.rs" and "config/mod.rs"
 --> main.rs:1:1
  |
1 | mod config;
  | ^^^^^^^^^^^
  |
  = help: delete or rename one of them to remove the ambiguity

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0761`.

The code is E0761, not E0583, and the header states the inverted problem: the file for the module was found at both candidate paths. This is the usual residue of converting a module from one layout to the other — the new file created, the old one never deleted. The help is the whole fix: delete or rename one of the two files, keeping the one that holds the current contents. The pair of errors brackets the rule from both sides — neither candidate present is E0583, both candidates present is E0761, and exactly one present is the only state that compiles. That is also what separates both codes from a failed resolution of the module name itself: in E0583 and E0761 the declaration resolved to a module the compiler knows it should load; the failure is purely about which file on disk supplies it.

The exact rule: a semicolon declaration is resolved to one of two files beside its declaring file

The official page for E0583 describes the code in one sentence: a file wasn't found for an out-of-line module. Its erroneous example is a single mod file_that_doesnt_exist; declaration, and its instruction is to be sure that a file corresponding to the module exists — to use a module named file_that_doesnt_exist, you need a file named file_that_doesnt_exist.rs or file_that_doesnt_exist/mod.rs in the same directory.

The Reference's page on modules states the resolution rule behind that instruction. A module without a body is loaded from an external file, and when the module does not have a path attribute, the path to the file mirrors the logical module path: ancestor module path components are directories, and the module's contents are in a file with the name of the module plus the .rs extension — so crate::util::config, declared from util.rs, lives at util/config.rs. The same page gives the alternate form: module filenames may also be the name of the module as a directory with the contents in a file named mod.rs within that directory — and it is not allowed to have both util.rs and util/mod.rs, which is the ambiguity E0761 reports.

The same Reference page covers the #[path] repair: the directories and files used for loading external file modules can be influenced with the path attribute, and for path attributes on modules not inside inline module blocks, the file path is relative to the directory the source file is located in. That relativity is why #[path = "shared/settings.rs"] in main.rs resolved beside main.rs — the attribute's path starts from the declaring file's directory, the same anchor the default search uses.

The fixes, ranked

  • First, read the help line in your own error and create the first file it names — config.rs beside the declaring file. This is the standard layout, the one the compiler suggests first, and the right default for a module that was always meant to have its own file.
  • Second, if the project already uses the directory layout, create the second named file instead — config/mod.rs. Either candidate satisfies the declaration; match the convention the surrounding modules already follow rather than mixing layouts at one level.
  • Third, when the declaration sits in a non-root file, create the file at the path relative to that file — util/config.rs for a mod config; inside util.rs — not beside the crate root. The help line already prints the correct full path; copy it rather than re-deriving it.
  • Fourth, when the module's code already exists under another name or in another directory, put a #[path = "..."] attribute on the declaration and point it at the real file, relative to the declaring file. Do not copy the code to the expected path; a second copy is a second module waiting to drift.
  • Fifth, when no separate file was ever intended, rewrite the declaration as an inline module — mod config { ... } with the body in braces. The module path the rest of the code uses does not change.
  • Do not create both candidate files to cover the bases. Both present is E0761, an ambiguity error — exactly one of the two paths must exist.

Which fix for which cause

Cause Message Fix
No module file exists E0583, file not found for module config, help names config.rs or config/mod.rs Create config.rs beside the declaring file
Project uses the directory layout E0583, same declaration span Create config/mod.rs instead
Declaration in a non-root file E0583, span in util.rs, help names util/config.rs or util/config/mod.rs Create the file relative to the declaring file
Code already exists elsewhere E0583, file present under another name or directory Add #[path = "..."] to the declaration
No separate file intended E0583 after a restructure left a semicolon declaration behind Rewrite as an inline mod config { ... }
Both candidate files present E0761, file for module found at both paths Delete or rename one of the two files

Checklist

  • Read the help line before creating anything. It prints the exact two paths the compiler checked, already resolved relative to the declaring file — creating a file at any other path leaves the error untouched.
  • Check which file holds the declaration. The span's file name is the anchor: a declaration in util.rs searches under util/, not beside main.rs, no matter where the crate root is.
  • Create exactly one candidate. config.rs or config/mod.rs, never both — both is E0761.
  • If a file with the module's code already exists somewhere, prefer #[path] over copying it. One file, one module; a copy compiles today and diverges tomorrow.
  • If the error's note mentions a mod of the same name elsewhere in the crate, stop before creating a file: the declaration itself is likely the mistake, and the fix is use crate::... to import the existing module.
  • After the fix, confirm the program actually uses the module. The fixed programs here print the value returned by the module's function, from call sites that never changed.

Sources

  • Rust Compiler Error Index, E0583 — for a file not being found for an out-of-line module, and for needing a file named after the module with the .rs extension or a mod.rs inside a directory of that name, in the same directory.
  • The Rust Reference, Modules — for a module without a body being loaded from an external file with the path mirroring the logical module path, for the mod.rs directory form and the rule against having both forms, and for the path attribute influencing the loading directories with paths relative to the directory the source file is located in.

Keep Exploring