Would you consider using #[path] for custom module naming bad practice?

Note: This is my first post on this forum, so apologies if this doesn't belong here. I've been using Rust for about a year now, so I'm excited to participate in discussions about it!

Currently, there are 2 possible ways to name a module file. Either src/name.rs or src/name/mod.rs. After some experience in Rust, I've come to prefer the latter approach. I like keeping code that belongs together in the same directory. In my opinion this makes more sense logically, and it looks better. The part I hate, though, is the file name mod.rs. I'd like to use just name.rs, so it ends up being src/name/name.rs.

This issue has been discussed before, the first example I found would be this thread. It seems the general consensus was against including this in Rust. I don't agree, but my question isn't about making this a language feature, my question is: is using #[path] for normal modules bad practice? So the resulting code would look like this:

#[path="name/name.rs"]
pub mod name;

I don't see anything inherently bad with this. While it may be unusual, it's pretty self explanatory, and while it adds some boilerplate, it has improved my developing experience substantially enough to start using this pattern in my whole project. I probably wouldn't do this in production code, but I've stuck to it now and I'm loving it!

Context: I've finally gotten to writing a bigger project in Rust, so I have a lot of (sub) modules. Using mod.rs everywhere became very confusing to me, e.g. as mentioned in the linked thread, having open multiple tabs in VS Code sucks. Visually, it also makes finding the correct file in a file viewer hard. I've thought of tooling/scripting as another point (e.g. fding an source file), but I have no example of where this is an issue.

What are your thoughts on this pattern? Do you think it'd be possible to include in the language? Should it? Thanks for reading this far :​D, have a great day everyone

It is a deviation from Rust's standard behavior, so other people browsing your project may be surprised it doesn't map to modules in the usual way.

Rust/Cargo's philosophy tends to be convention over configuration, so I'd suggest sticking to defaults if you don't have a need stronger than just tweaking things to your liking.

1 Like

Reminder that you no longer need to use mod.rs: https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html#no-more-modrs.

1 Like

I know that, my question is about having name.rs inside the name folder rather than outside

Thanks for your input. I agree this 100% shouldn't be used in prod, but I'm still not sure if it's a good idea for personal stuff

It's certainly an uncommon practice. But I'd say using a different file path is only bad if it's done without or for the wrong reasons. If the readability increase is a good enough reason is really up to you.

I'd first look at ways to customize VSCode. There might be a way to give the mod files better labels, but I don't know of any. This would also help you with other people's projects, which is why I'd have a look at that first in any case.

If you decide to use different paths, and since it's likely people will not expect it, I'd suggest:

  • Perhaps use name/mod_name.rs or similar instead of simply name/name.rs. This has a higher chance of being understood as simply a named mod.rs, will show up at the expected position in file listings, and so on.
  • Drop a README.md in the src/ directory explaining the convention you're using for those browsing, and a comment in your crates' top level source file.
1 Like

Sublime text does a nice job here. If I open a couple mod.rs files from library/alloc, I get this: image

Dunno if there's a way to get that in VS Code, though.

VSCode also has that:

image

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