What Is the Difference Between mod.rs and Named Files in Rust 2018+?

Rust 2018+ allows modules to be defined by a single named file (e.g., utils.rs) without requiring a mod.rs file inside a directory.

In Rust 2018+, mod.rs is no longer required for inline modules or when using the mod name; syntax with a same-named file, whereas named files (e.g., mod.rs replaced by name.rs) are the standard way to define modules in modern editions. The compiler now automatically looks for name.rs or name/mod.rs when you declare mod name;, making the explicit mod.rs file optional unless you are using the directory-based module structure.

// src/lib.rs
mod utils; // Compiler looks for src/utils.rs OR src/utils/mod.rs

// src/utils.rs (Named file approach - preferred in 2018+)
pub fn helper() {
    println!("Helper function");
}