The question seems have gone further to "Is it possible to avoid both mod.rs
and <directory>.rs
" or "if neither exist but ./<directory>
(or ./<directory>/<file>
) exists, why not then treat mod name;
as the same as if we had an empty ./<directory>/mod.rs.
" as @allidoiswin has mentioned.
Such a file was probably unavoidable. This could be because a directory only indicates the presence of a node in the module tree and does not describe the node. However, mod
in a .rs
file can fully describe the information of the node(e.g. functions in the mod, the visibility of the submodule).
Example
Suppose we want to move the mod house
in main.rs
to a directory.
.
└── main.rs
// main.rs
mod house {
mod guest {}
pub mod host {
pub fn clean() {
super::clean_house();
}
}
fn clean_house() {
println!("Cleaned!");
}
}
So we make the directory this way and want to avoid house.rs
.
.
├── house
│ ├── guest.rs
│ └── host.rs
└── main.rs
// main.rs
mod house;
fn main() {
house::host::clean();
}
// host.rs
pub fn clean() {
super::clean_house();
}
But we found no where to write the the clean_house()
function and give visibility to mod host
, because house
is a directory rather than a rust code file.
And the solution is to add a house.rs
file, which provides the extra information of the directory house
.
.
├── house
│ ├── guest.rs
│ └── host.rs
├── house.rs
└── main.rs
// house.rs
mod guest;
pub mod host;
fn clean_house() {
println!("Cleaned!");
}
If we consider that house
and house.rs
are co-active, and that the house
directory is where submodules of house.rs
are stored, then there may be some consistency.