Module/mod.rs or module.rs?

I remember reading somewhere (a post here or a blog) that the suggested way to organize submodules is to place the root of the submodule in module.rs and the rest of the files in the module directory. I.e.,

foo.rs
foo/bar.rs

is better than:

foo/mod.rs
foo/bar.rs

Unfortunately I cannot find that post anymore. The question is if there really is an "official" way to organize submodules (and a rationale) or it is just personal taste?

1 Like

From the docs:

Note: Prior to rustc 1.30, using mod.rs files was the way to load a module with nested children. It is encouraged to use the new naming convention as it is more consistent, and avoids having many files named mod.rs within a project.

1 Like

The docs... obviously. /me hides in shame. :slight_smile:

I disagree with the "official" guidance. Feel free to use whichever one you prefer. In Tokio, we mostly use mod.rs files, since it just makes more sense to have fs-related code inside the tokio/src/fs directory. Having part of the module next to the directory isn't sensible to me.

21 Likes

I will always ask and yearn for foo/foo.rs, the best of both worlds.

3 Likes

There's a recent and related thread on IRLO:

1 Like

Just for completeness we should also add the worst possible solution which forces all module files into a folder called mod, i.e. mod/foo.rs :joy:

4 Likes

And highlighting the most useful comment for me.

4 Likes

I don't have a strong opinion what the official guidance should be, but if we all follow the same guidance, it reduces confusion and mental burden.

Sure, consistency is good. Tokio is consistently using mod.rs.

That's why I asked. I must say that I don't like the module.rs / module/ mainly because there is no way to keep them together in the IDEs I use.

I would usually agree, but in this case I don't. The two schemes have pros and cons that affect different people/projects in different ways. Moreover, in this case, I don't feel that the cognitive burden is very high: with a little thought and investigation, one could learn about either scheme just by looking at source code. And that's only if you didn't read the Rust docs which explain both.