HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustMajor

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
themodpurposerustfilewhat

Problem

In some Rust projects I've seen (i.e pczarn/rustboot), I've seen mod.rs files in directories for whatever reason. I've not been able to find documentation about this, and I've seen it in many other Rust projects.
What is the purpose of a mod.rs file, and when should I use it?

Solution

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.

Context

Stack Overflow Q#26435102, score: 85

Revisions (0)

No revisions yet.