Greetings,
I recently went back to work on the few of projects written in Rust and decided to renew them to the newer editions (was pre/half-pre 2018 based).
During the work, I noticed you folks has turned away from the mod.rs
file, and now encouraging the use of ./module
+ ./module.rs
approach instead. The 10.5. File hierarchy section of the Rust By Example book don't even mention mod.rs
anymore, and 7.5. Separating Modules into Different Files is calling it "older style".
As I understand it, the main ground for the switch was to avoid confusion of editor tabs? Well... if true, then why should it be addressed by changing a programming language? Shouldn't it be a flaw of tabbed text-editor and needs to be addressed by their developers?
There are countless files named .DS_Store
, Desktop.ini
or Thumbs.db
in created by the operating systems automagically in everyone's computer, and you don't see it bothers Apple or Microsoft.
So yea, I don't like the idea.
When I started learning Rust, I pictured mod.rs
as a special manifest that exposes/lists the structure of a module, just like how main.rs
or lib.rs
introduces the structure of a program (that's the files you should be reading first time you got the code).
I also picture Rust's module file hierarchy system as "directory based", in my mind, everything of a module should be contained in one directory, including mod.rs
and whatever you have in that module. That's also how most people use normal folders: put everything related neatly inside.
My first problem with ./module
+ ./module.rs
is that, the ./module.rs
is outside the ./module
folder, so now you have two place to look.
Another problem (actually, related to the first problem) is that, now even when ./module.rs
is outside of ./module
, it could still effects how things work inside ./module
.
For example, say you have a module structured as following:
.
βββ module
β βββ a.rs
β βββ b.rs
βββ module.rs
in file a.rs
, you define pub fn a()
, in file b.rs
you have
use super::a::a;
pub fn call() {
a();
}
and in module.rs
you have
mod a;
mod b;
pub use b::call;
Everything is fine right now, but imagine somewhere down the line you made a mistake by removing the mod a;
line from ./module.rs
which is a file located outside of ./module
, then... guess what the editor will tell you there is a mistake in ./module/b.rs
which is located inside ./module
. For normal people, this is very confusing because why should a edit done outside of a folder causes error inside the folder? That's just not how scope works normally.
So to make people understand the whole thing, you'll have to put in additional effort to educate them, i.e. more cliff to climb.
Speak of confusion, here's my third problem: back in the day, everybody knows that mod.rs
is a special file, just like main.rs
and lib.rs
. It's a relatively easy mental model to remember.
Now every .rs
file you see could be a mod.rs
in disgust. But normal .rs
and mod.rs
don't work the exact same way, so some day, someone will ask "why is my mod something
statement would compile in one file, but stops working as soon as I moved it to the next file?", so... more cliff to climb?
(also, dummy, that mod something
in one.rs
imports something completely different if you put it into two.rs
, assuming ./one/something.rs
and ./two/something.rs
both exist)
OK, after all of that, what I'm trying to say is: there is no question that the ./module
+ ./module.rs
approach is here to stay (I hoped not, but here we are). But, can we also support mod.rs
at the same priority? Not "just an oldstyle", not "discouraged", but "another way/style"?
Reading around the Internet, there are quite a few stubborn mod.rs
supporters around Link 1, Link 2. I think I'll keep using mod.rs
too if that's OK.
And that's all