Let Cargo recognize source files other than *.rs

Is it possible to configure Cargo so that it expects the Rust source file extension to be something different than ".rs"? Because I, for example, very much prefer ".rust".
So, I want to write something like "mod MyMod;" and implement it in a file called "MyMod.rust".

Related question: Does anybody know what the rationale was behind making ".rs" the default file extension? I'm really curious.

I imagine for someone who has to decide on a "default" file extension for Rust files, the very first idea that must come to mind is ".rust". Isn't that most straight forward, clear, logical? You know at once what the file is concerned with or can easily google it.
The only (weak) point I see in not using ".rust" is to shorten it. But why would one need a file extension even shorter than four letters?? (The days of the three-letters-limit are long over; there are ".kdenlive" files and it's just fine.) Honestly, one barely has to type a whole file name anyway; there is tab-completion on the shell, clicking on file names in an IDE, tools like Cargo that add the extension automatically (see above), etc.; expressiveness is much more important I'd say.

Also, if one really wanted an abbreviation for ".rust", why not use one of the typical schemes like using only the first letters (as in prof for professor) which would have lead to ".rus" or ".ru" (I know that ".r" is already taken) or the first and the last letter (as in dr for doctor) which would have lead to ".rt"?

I'd also say it's "good style" (manners?) to use the name of ones application as file extension for
any new type of file that is used by that application (even if the "application" is a programming
language (i.e. compiler/build tool)). This way one doesn't take away a file extension for a better suited application. Imagine for example that sometime a language called Rustscript comes up
(just like with Java and Javascript) -- it would be a shame if the respective files couldn't have
the file extension ".rs"! By the way, Java source files also end in ".java" not ".jv" ...

Sorry, that this got so verbose but Rust is still a young language and I personally hope that ".rust" will become the most frequently used extension for Rust source files.

You could try use use the #[path] option:

#[path="MyMod.rust"]
mod MyMod;

This is documented here: The Rust Reference has moved

On the more general subject of file extensions, I can't comment on the exact history and choice of .rs but I will note that using a short 1- or 2-letter extension is common in the world of programming languages: .hs for Haskell, .py for Python, .pl for Perl, .ml for OCaml, .js for JavaScript, .rb for Ruby, etc.

There are of course counterexamples to this (like .scala), but if I had to speculate, I'd guess that the rust designers felt comfortable following this trend.

2 Likes

Thanks! I guess that's the best solution I could hope for.

To be honest, I wasn't aware of that "trend" concerning two-letter extensions for programming languages, I only had all the counter-examples in mind, like Java, Scala or Lua (I'm glad they didn't establish .lu files). I accept, though, that you have to draw a line somewhere, concerning the length of a file extension, but for me it is definitely something beyond four letters (*.py for Python scripts is well justifiable).

Note that the "trend" is quite old and is not only two-letter suffixes but anything up to three letters. Older filesystems on CP/M or DOS only had 11 bytes for filenames, which were (probably by convention) split into 8 ascii chars for the name and 3 for "extension".

In order to stay compatible with those systems, no more than 3 letters should be chosen, so when there was no reason against it, this has stayed the default.

2 Likes