Modules named

Can someone tell me the rationale behind having to name files rather than their actual name. I seem to be ending up with several files called albeit in different directories which is confusing in the editor. Apart from putting everything in the crate root I can't seem to avoid having a in say a /protocol directory and have it just have mod statements to include the modules by name that I actually want in that directory or if I move two modules and from /protocol into their own directories I again have to call them Am I missing something here?

Can you provide the output of find src so we know what your source tree layout looks like ?

You do not strictly have to name any file; for any module file there are 3 alternatives:

  1. directory_of_the_parent_module/protocol/
  2. directory_of_the_parent_module/
  3. Any path you like using the #[path] attribute (don't do this unless you have a serious reason, because it's confusing)

(Here directory_of_the_parent_module is src/ if the module is a child of the root.)

It is true, however, that there is no standard location for a module that is both inside "its own directory" and not named I recommend getting used to option 2. It has the advantage that creating child modules does not demand relocating the module's own source.


That was a change in the 2018 edition:

1 Like

With the 2018 edition and later, you can freely rename your protocol/ file into without additional work.


Applying the Edition 2018 approach to this situation, you'd have something like this:

pub mod protocol;

pub mod encoder;
pub mod decoded;


pub mod enc1;


// Rust code...


pub mod dec1;


// Rust code...
1 Like

Thanks for 2018 tip. That seems to work with having a level of indirection in the root so now I will change everything and get rid of all the files.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.