Bevy: Refactor {module}/mod.rs to {module}.rs?

Created on 6 Oct 2020  路  6Comments  路  Source: bevyengine/bevy

Since, from rust 2018 mod.rs is no longer required, the file structure can be changed. Pls add your opinion.

Most helpful comment

My real preference is to have implicit mod.rs and directly export public symbols from each file in a folder. But sadly rust chose to support every option but my preference

The following is similar (not implicit though) and is my favorite option for module organization of smaller crates. This is especially nice when a module_name.rs or module_name/mod.rs creates more file clutter than its worth only to export some items.

src/
- lib.rs
- foo/
   - a.rs
   - b.rs
- bar/
  - c.rs
  - d.rs
// lib.rs

// no need for foo.rs or foo/mod.rs
mod foo {
    pub mod a;
    pub mod b;
}

// keep the internal modules private
// and place all public items in bar
mod bar {
    mod c;
    pub use c::*;
    mod d;
    pub use d::*;
}

All 6 comments

{module}/mod.rs encapsulates the module hierarchically, keeping all of it on the same level (directory/folder/package/whatever). {module}.rs smears it across at least two levels. Doesn't matter if the file contains actual code or only re-exports.

Yeah my preference is to keep it as-is to keep it all at the same level.

My _real_ preference is to have implicit mod.rs and directly export public symbols from each file in a folder. But sadly rust chose to support every option _but_ my preference 馃槄

mod.rs is annoying. Spreading the module across two directories _without_ mod.rs is _also_ annoying. At least VS Code postfixes (and IntelliJ prefixes) file tabs with the parent directory when the file name is the same -- that helps somewhat in the mod.rs case. Not so long ago I remember just having a sea of bare mod.rs files. What a nightmare.

I'd love to see a {module}/{module}.rs option in Rust! Best of both worlds, IMO.

My real preference is to have implicit mod.rs and directly export public symbols from each file in a folder. But sadly rust chose to support every option but my preference

The following is similar (not implicit though) and is my favorite option for module organization of smaller crates. This is especially nice when a module_name.rs or module_name/mod.rs creates more file clutter than its worth only to export some items.

src/
- lib.rs
- foo/
   - a.rs
   - b.rs
- bar/
  - c.rs
  - d.rs
// lib.rs

// no need for foo.rs or foo/mod.rs
mod foo {
    pub mod a;
    pub mod b;
}

// keep the internal modules private
// and place all public items in bar
mod bar {
    mod c;
    pub use c::*;
    mod d;
    pub use d::*;
}

Whoa hold on that sort of changes everything. I didn't know we could do that :)

Closing this issue since there are many ways to organize files in Rust. Feel free to add your way of organizing files that might be helpfult to others

Was this page helpful?
0 / 5 - 0 ratings

Related issues

w1th0utnam3 picture w1th0utnam3  路  3Comments

Bauxitedev picture Bauxitedev  路  5Comments

TheArchitect4855 picture TheArchitect4855  路  3Comments

ahfuckme picture ahfuckme  路  4Comments

vultix picture vultix  路  3Comments