mod.rs)
Rule
For a module named foo:
- Preferred:
foo.rs(module root) andfoo/(child modules live here asfoo/bar.rs, etc.). - Avoid:
foo/mod.rsas the root offoo. Do not introduce newmod.rsfiles; migrate away when touching that area.
lib.rs / main.rs follow the same idea: mod foo; resolves to foo.rs or foo/mod.rs — choose foo.rs + foo/.
Layout sketch
src/
lib.rs
foo.rs # pub mod bar; mod util;
foo/
bar.rs
util.rs
Not:
src/
lib.rs
foo/
mod.rs # avoid
bar.rs
When editing or adding modules
- Add submodules as
parent/child.rsand declare them inparent.rs(the file next to theparent/directory). - If you see
path/to/mod.rs, plan a move topath/to.rsbesidepath/to/(same module tree, different root file location). Keepmoddeclarations andpub usebehavior equivalent. - Do not create new
mod.rsfor convenience or “convention from old tutorials.”
Exceptions (rare)
- Generated code or vendored third-party trees — leave as-is unless the user asks to normalize.
- If the project’s documented standard still requires
mod.rs, follow the repo; otherwise apply this skill.
Doc anchor
Rust book: modules can be module_name.rs or module_name/mod.rs; prefer the module_name.rs + module_name/ form for new work and refactors.