r/rust•
apemanzilla

mod.rs in Rust 2018: yes or no?

Rust 2018 changed the way modules work so that instead of having a module defined as <module>/mod.rs with other submodules in the <module>/ directory, you can now use <module.rs> outside of the directory, but keep submodules in <module>/. I've tried both ways, but I think I still prefer doing things the old way with mod.rs, since it keeps the entire module in a single, self-contained directory, which seems a bit more organized to me than putting them side-by-side. What does everyone else think?

Posted Mar 27, 2019, 2:58 PM UTC

3838
burntsushiripgrep · rust•

I haven't done too much with Rust 2018 yet, but I expect I'll stick to mod.rs. I prefer containing everything in the directory.

29
0b01•

Depends on the content of mod.rs. If it's empty or some cursory re-export statements, I'd put it in the same directory for modularity's sake. Otherwise it should warrant its own file.

1
jx9hc•

Otherwise it should warrant its own file.

Huh? It's its own file either way...

4
burntsushiripgrep · rust•

My mod.rs files rarely have anything other than re-exports. (At least, that's my memory speaking. I've written a lot of mod.rs files, so I wonder how many have more than just re-exports.)

In any case, your advice seems orthogonal to the OP's question...

1
sirak2010•

I came from Java and I still don't get why we need mod.rs can't it be just the filename and the directory structure.?

1
burntsushiripgrep · rust•

"need" is such a harsh word. Every module has some root to it, so if you want to put a module into a directory, you need some place for the root of the module to exist. In Rust 2015, that place was mod.rs inside of the module sub-directory. I personally like that style because it serves as a nice sentinel and keeps every part of a module contained in a single sub-directory. Rust 2018 added another way to do this, where if you have a directory for a sub-module foo, then you can put the root of that module in foo.rs instead, where foo and foo.rs are siblings in your directory hierarchy.

3
Blando-Cartesian•

But how come directory with .rs files isn’t implicitely a module if mod.rs is mostly just used to declare submodules?

1
K900_•

I prefer ${name}.rs for small modules that are largely self-contained and ${name}/mod.rs for large modules that are split into multiple files. That's how Python does it, too.

25
apemanzillaOP•

Right now I've been following a similar policy: If the entire module is defined within one file, keep it in <module>.rs, otherwise, use <module>/mod.rs.

13
tim_vermeulen•

So basically the same as in the 2015 edition?

5
apemanzillaOP•

Pretty much.

2
kibwen•

A person's preference will depend on whether they prefer a bird's eye view approach to file organization or a mole's eye view approach. The old way leads to clean directory structures, which is why it was originally chosen, but in practice it means editors will often have a dozen tabs open all unhelpfully labeled "mod.rs", which is often needlessly frustrating. I prefer the newer approach for this reason, since lack of navigational ease frustrates me more than lack of self-containededness.

22
apemanzillaOP•

That's a very good point. In CLion, the files will be disambiguated if necessary (module1/mod.rs, module2/mod.rs, etc), but it's still a bit of a nuisance.

4
loonyphoenix•

Even in sophisticated IDEs, files named the same are generally an annoyance. If you try to use something like a "go to file" command, the IDEs usually consider the name of the file first and its place in the directory hierarchy second. So if you want to go to module foo located in foo.rs, you can just press whatever shortcut "go to file" functionality is mapped to and type foo, and this is generally enough for the file you seek to be at the top of the list. If you try to do the same with mod.rs, it doesn't always work. Obviously this could be worked around by the IDEs by special casing this scenario somehow... In any case, I think I prefer the foo.rs approach for now.

3
mathstuf•

Shouldn't language servers solve this since they have much more information than a simple analyzer? Heck, even ctags should have full path information for declaration info.

1
seamsay•

I feel like this is a non-issue in practice because most modern editors implement fuzzy search when doing "go to file", so if you want to got to foo/mod.rs you would type fomo or fmod or something like that.

1
[deleted]•

[deleted]

1
kubkon•

This^ :+1:

1
kuikuilla•

No to mod.rs, simply because then any go to file action in any IDE and editor will not work that well.

7
shadow31•

I usually just type {part of mod name}/mod.rs and it works fine

8
kuikuilla•

Then there's also the fact that the editor will have tons of tabs (or whatever the program uses on its UI) that have the same "mod.rs" text.

3†
shadow31•

It's really not too much of an issue in most editors https://imgur.com/kCvl2v8

4
loonyphoenix•

That still takes twice as much space and is harder to read.

2
apemanzillaOP•

I hadn't considered that, I rarely use go to file - I usually either use go to declaration, which works regardless of file name, or just open the file from the sidebar.

5
GyscosCursive•

For Rust 2021, I think I might like a foo/foo.rs as alternative to foo/mod.rs. This way you get the clean directory structure, and the different filenames.

7
nicoburns•

I'm personally hoping for a foo/_mod.rs option so that it doesn't get lost in the middle of all the other submodules.

2
danaugrs•

Definitely the best of both worlds. Really hoping Rust 2021 will support this.

1
mmstickion•

I'm sticking to mod.rs for my projects. It's a good convention, for the reasons you stated.

4
craftytrickster•

The original mod.rs approach is significantly clearer for me, I personally find it confusing to have a folder and a file have the same name in the same directory.

4
CrazyKilla15•

I prefer your way too, i like everything contained, though i also see the benefit of the new way and may switch eventually.

If the entire module fits in the one file then of course no directory, too.

3
matkladrust-analyzer•

I don't find either one significantly better than the other, for reasons people outlined in this thread. So I'd just stick with what is considered the best practice. Unfortunately, as this thread shows, there are two different best practices.

I wonder how we ended up in this situation: shipping a feature, which is an alternative to an existing one, and which is not better, just different (note: this assessment is based primarily on this reddit thread and my gut feeling, it might be the case that mod.rs-less variant is significantly better for 90% of Rust programmers). To me, it seems clear that "just another way to do X" features should never be added to the language, unless "the new way" is order of magnitude better than the old one: language stability (as in, no new stuff) and orthogonality are super important. Basically, this comment by Graydon resonates strongly with me: https://internals.rust-lang.org/t/rust-2018-an-early-preview/7776/14

3
burntsushiripgrep · rust•

I agree. I realize this is a cliche, but one of the hardest parts about maintaining software is saying "no." And I don't say that as someone who is above such things; I fail at it all the time. It is sometimes daunting to even begin to figure out when exactly the right time is to "debate" whether we should do something at all. And even if you do, it is a very difficult role to play. (This isn't to understate the difficulty of any other role in feature development either, of course.)

3
swfsql•

I prefer the new way. It never worked for me to have an mod.rs stepping in between the module files. I also considered it a bad positioning, alphabetically-wise.

But this was quite a minor issue, which now I consider completely solved..

2
epagecobalt•

I think I still prefer doing things the old way with mod.rs, since it keeps the entire module in a single, self-contained directory, which seems a bit more organized to me than putting them side-by-side.

Do you keep logic in your mod.rs or does it just pull in the rest of the module and re-export things?

For me personally, I avoid having logic in mod.rs and lib.rs (and minimize my main.rs logic) so I don't think I'll mind <module>.rs since all of the logic will still be contained in the directory.

If I'm already splitting the logic up into multiple files, I want all of the logic split up into self-contained, named files rather than some being left behind. I find it makes it harder to predict where code will be when it is left in the top-level file for the module or library.

1
apemanzillaOP•

Unless it's very simple logic that's used by the submodules (usually a trait) I only use mod.rs for re-exporting. Despite this, I still find it feels more intuitive to use mod.rs within the directory rather than the new syntax, just because it keeps everything together.

3
bionicbits•

I am with you, coming from Go, I tend to like the modules in a single folder. That being said, most other Rust projects I have seen, tend to have modules in files in src as opposed to subdirs.

1
cmrx64rust•

I don't think OP is saying anything about src.

1
agmcleod•

I typically just have {file}.rs when it's a small enough module for one file. If it is a folder, i have preferred mod.rs for defining the sub modules of that, and for re-exporting. On my game project i have a {file}.rs and {file}/other.rs and it feels weird. https://github.com/agmcleod/spellcaster-sacrifice/tree/master/src screen/ and screen.rs there.

1