In Rust, what is the purpose of a mod.rs file?

Imagine the following directory structure:

code/
  `- main.rs
   - something/
     `- mod.rs

If in main.rs you do mod something;, then it’ll look in the something/mod.rs file to use as the contents of the module declaration for something.

The alternative to this is to have a something.rs file in the code/ directory.

So to recap, when you write an empty module declaration such as mod something;, it looks either in:

  • a file called something.rs in the same directory
  • a file called mod.rs in a folder called something in the same directory

It then uses the contents of either of those files to use as the contents of the module declaration.

Leave a Comment