Preferred filename for modules in Rust 2018


#1

So, Rust 2018 allows both foo/mod.rs, foo/bar.rs and foo.rs, foo/bar.rs. Which one is preferred style?

In other words, should I rename all my mod.rs to be consistent with recommended style, or is mod.rs still a preferred default?


#2

Haven’t read the book / GH issues to see what’s the recommended way, but from a “sometimes I write docs in mdbook” point of view, I’d go with:

root
|- submodule
|  |- foo.rs
|  |- bar.rs
|
|- submodule.rs

mainly because that aligns with:

book
|- section
|  |- subtopic.md
|  |- other.md
|
|- section.md

Also, if you have a Java-esque convention of naming types and files with the same name, it’s easier to find say, MyType inside a my_type.rs instead of my_type/mod.rs, especially when you don’t have an editor that indexes the types for you (saves a (rip)grep through a big-ish code base to find a type).


#3

IMHO, the new style is preferred. We wouldn’t have added it if it was worse.


#4

Thant’s what I thought. Perhaps the edition guide could be more clear about that? Is there an existing tool which can change mod.rs to new format?


#5

Maybe this (use at your own risk, I will be using this soon):

for f in $(find . -type f -name "mod.rs")
do
  echo mv $f ${f%%/mod.rs}.rs
done

Random test:

for f in something/abc/mod.rs
do
  echo $f ${f%%/mod.rs}.rs
done

gives:

something/abc/mod.rs something/abc.rs