Mod declaration confusion

I moved a to and the [mod x;] declaration in it that worked fine now attracts an 'x not found' error.
but sits right there in the same directory so how can this possibly be wrong now?

The problem is not clear with the mod structure/layout before and after unknown.

It just reads to me that "I fiddled with some code and files, and it doesn't work any longer".

My guess from what you've said is that you had this:


Now, you have this:

src/ -- or maybe a *new*

If that is the case, then mod x; inside of would indeed be invalid.

The best solution to this would be to read the documentation regarding modules in Rust, and how the files need to be structured. I'll try to summarise here: and, when they are the root module of a crate behave slightly differently to other modules. Their "search path" is the directory they are currently in.

When you have a submodule mod lib_static; inside of, it will look for the file as either src/ or src/lib_static/

If you try to put a mod x; submodule in, however, it will look for src/lib_static/ or src/lib_static/x/ Basically, once you're not talking about or, submodules are expected to be in a subdirectory named after the module that contains them.

Or, in other words, you can't have an entire module tree in a flat directory. Well, you can, but you have to override paths for that to work, and you really shouldn't do it because it will just confuse everyone else looking at your code. :stuck_out_tongue:

The solution is to move to lib_static/


I thought it more as avoiding superfluous detail.
it is the simplest possible arrangement:

I renamed to with macro filled
with the content of plus extras.
so no change to other than name for the following result:

error[E0583]: file not found for module `x`
  --> lib/src/
16 | pub mod x;
   = help: to create the module `app`, create file "lib/src/lib_static/" or "lib/src/lib_static/x/"
   = note: if there is a `mod x` elsewhere in the crate already, import it with `use crate::...` instead

which suggests to me there is a difference in status of the main library crate file versus supporting files.

Actually, this might make it clearer:

Back before, I think it was the 2018 edition of Rust, all modules which contained other modules were required to be laid out like this:


That is, if you had a "module-of-modules", it had to be in $name/ In this context, and are just special-cased versions of they are files that represent the root module of the crate, and that module's contents are in that directory.

From 2018 on, you were allowed to name these "module-of-modules" files as $ in the directory of the parent module. However, their child modules still had to be in that subdirectory.

So, yes, is special, but less special than you think. It's actually which is more special, but in a non-obvious way.

(For context: I never liked this change, and felt it would make the module system harder to understand. :P)

Edit: the above ignores the existence of inline modules, because I can't be bothered to explain those as well right now.


Here's the relevant portion of the reference.


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.