patternrustMajor
In Rust, what is the purpose of a mod.rs file?
Viewed 0 times
themodpurposerustfilewhat
Problem
In some Rust projects I've seen (i.e pczarn/rustboot), I've seen
What is the purpose of a mod.rs file, and when should I use it?
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/
If in
The alternative to this is to have a
So to recap, when you write an empty module declaration such as
It then uses the contents of either of those files to use as the contents of the module declaration.
code/
- main.rs
- something/
- mod.rsIf 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.rsin the same directory
- a file called
mod.rsin a folder calledsomethingin 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.