# Allow self-named mod files

**URL:** <https://internals.rust-lang.org/t/allow-self-named-mod-files/3033>\
**Category:** language design\
**Created:** [December 28, 2015, 11:45pm UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033 "2015-12-28T23:45:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [December 28, 2015, 11:45pm UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/1 "2015-12-28T23:45:48Z")

</div>

[This comment](https://www.reddit.com/r/rust/comments/3yihdy/whats_everyone_working_on_this_week_eo15/cye6pjy) raises a suggestion that seems workable to me, and that might make Rust’s module system easier to understand for people coming from other languages: allow `/foobar/foobar.rs` to be treated as `/foobar/mod.rs`. To avoid conflict, if `/foobar/mod.rs` exists, it would have to take precendence as the mod file, but if that file isn’t found we could fall back to `/foobar/foobar.rs`. This seems more similar to how many other languages work and I don’t see any way it would be incompatible or conflictual. Thoughts?

More broadly, Rust’s module system seems to be the cause of far more confusion than it deserves, given its limited inherent complexity, and figuring out ways it could behave more like how people expect seems like a good idea.

---

<div class="post-metadata">

**Author:** ![seanmonstar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seanmonstar/32/36_2.png) [@seanmonstar](https://internals.rust-lang.org/u/seanmonstar)\
**Post date:** [December 29, 2015, 12:31am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/2 "2015-12-29T00:31:03Z")

</div>

The current design is similar to other languages that I know. With Python, you either use `foo.py`, or `foo/ __init__.py`. In Nodejs, you use `foo.js` or `foo/index.js`.

For your example, why wouldn’t you just write `/foobar.rs` instead of creating a folder?

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [December 30, 2015, 5:27am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/3 "2015-12-30T05:27:57Z")

</div>

I like that suggestion a lot. Alternatively, instead of requiring “/” to separate modules in paths, allow “.” to separate modules in paths. That is, instead of `src/foo/mod.rs` and `src/foo/bar.rs`, we should be able to use `src/foo.rs` and `src/foo.bar.rs`.

Note that for the same reason that the original poster cited, I usually don’t use `lib.rs` as the name of the main library module file. Instead, I usually use .rs. If I used `lib.rs` for everything then I’d have 10 `lib.rs` files open in my editor right now, with no easy way to quickly discern one from the other.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [December 30, 2015, 7:21am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/4 "2015-12-30T07:21:47Z")

</div>

> [@seanmonstar](#):
>
> For your example, why wouldn't you just write /foobar.rs instead of creating a folder?

Probably because you want it to have submodules. An issue with `mod.rs`, which @briansmith alludes to, is that in many editor's interfaces emphasize file name without the directory path, so its hard to differentiate between your module files.

I'm not really sure if this is a good suggestion or not, but I think _something_ should be done that would make Rust's modules easier for people to understand.

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [December 30, 2015, 8:17am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/5 "2015-12-30T08:17:08Z")

</div>

I’m not sure if this will make Rust’s module system _easier_ to understand. It doesn’t address _any_ of the inherent complexity of the system; it just bolts _yet another rule_ on to it, thus making it _more_ complex.

Also, who is going to expect this? I cannot think of _any_ language I’ve used where the described convention was used. On the other hand, Python immediately jumps to mind as a language that does more or less the same thing Rust does.

Really, 90% of the problems seem to stem from people conflating `mod` and `use`. That’s not because Rust is bad, just that it’s different. I don’t know what more can be done, since it seems to be explained pretty thoroughly and carefully. At this point, I’m increasingly inclined to think people just aren’t reading the docs and are _guessing_ at the semantics.

Finally, the editor thing is more a failing of the editor than of Rust. Not having a way to distinguish identically-named files in different directories seems like a design flaw. 😛 If it’s not something you can address on the editor side of things, you could always force a fix by using `#[path]`.

---

<div class="post-metadata">

**Author:** ![jibal](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jibal/32/1353_2.png) [@jibal](https://internals.rust-lang.org/u/jibal)\
**Post date:** [December 31, 2015, 11:12pm UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/6 "2015-12-31T23:12:16Z")

</div>

> I cannot think of any language I've used where the described convention was used.

seanmonstar gave two examples.

[edit: as sfackler noted, I got this backwards.]

> Finally, the editor thing is more a failing of the editor than of Rust.

This "let them eat cake" attitude is fundamentally wrongheaded. Whether editors should be able to squeeze full filenames onto little tabs and not doing so is "a design flaw" is irrelevant ... people should not have to redesign the editors they use in order to use Rust effectively.

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [January 1, 2016, 12:43am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/7 "2016-01-01T00:43:23Z")

</div>

@jibal seanmonster’s examples were of languages with similar setups to Rust today, not to this proposal.

---

<div class="post-metadata">

**Author:** ![skade](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skade/32/3333_2.png) [@skade](https://internals.rust-lang.org/u/skade)\
**Post date:** [January 1, 2016, 10:28pm UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/8 "2016-01-01T22:28:15Z")

</div>

> [@jibal](#):
>
> This "let them eat cake" attitude is fundamentally wrongheaded. Whether editors should be able to squeeze full filenames onto little tabs and not doing so is "a design flaw" is irrelevant ... people should not have to redesign the editors they use in order to use Rust effectively.

But this is not an issue of Rust alone. Many programming environments favour repeated naming in a directory structure over different file names for every component. For good reasons, I might add.

So it _is_ an editor issue, in and outside of Rust. Just as Rust shouldn't force your editor, command-T also shouldn't force Rust.

---

<div class="post-metadata">

**Author:** ![jibal](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jibal/32/1353_2.png) [@jibal](https://internals.rust-lang.org/u/jibal)\
**Post date:** [January 1, 2016, 11:33pm UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/9 "2016-01-01T23:33:20Z")

</div>

I disagree on all counts, but I won’t pursue it further.

---

<div class="post-metadata">

**Author:** ![Azerupi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/azerupi/32/1068_2.png) [@Azerupi](https://internals.rust-lang.org/u/Azerupi)\
**Post date:** [January 2, 2016, 10:02am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/10 "2016-01-02T10:02:08Z")

</div>

> [@withoutboats](#):
>
> many editor's interfaces emphasize file name without the directory path, so its hard to differentiate between your module files.

Most of the time when I have modules that deserve a directory of their own and thus use the name `mod.rs`, I use the `mod.rs` file only to define other files (modules) and re-export. The `mod.rs` file is therefore very short and doesn't change a lot.

Example of a typical `mod.rs` file from a project of mine.

```rust
pub mod mdbook;
pub mod bookitem;
pub mod bookconfig;

pub use self::bookitem::{BookItem, BookItems};
pub use self::bookconfig::BookConfig;
pub use self::mdbook::MDBook;

```

So the `mod.rs` file doesn't need to be opened a lot unless you add or refactor modules. This mitigates the editor problem.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/allow-self-named-mod-files/3033/11 "2019-03-25T08:25:37Z")

</div>

Closed on March 25, 2019, 8:25am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
