Libraries are imported as dependencies in main.toml file,however in this case the submodules are not imported as expected.
Existing Modules(output_0.rs/mod_1/output_1.rs) are not recognized as part of library(mods_dir/lib.rs).
error[E0583]: file not found for module `output_0`
--> mod_dirssrclib.rs:18:5
|
18 | pub mod output_0;
| ^^^^^^^^^^^^^^^^^
|
= help: to create the module `output_0`, create file "mod_dirssrcmods_diroutput_0.rs" or "mod_dirssrcmods_diroutput_0mod.rs"
EDIT : Reuploaded a clearer image showing main.toml where the mods_dir.lib get imported as a dependency. –
2
Answers
Directories need not be included as modules when they have a .lib file associated with them ,as they are directly recognized by the compiler. However, they need to be added to the main.toml as a dependency so that the lib directory's content get associated it and compiled along with the bin file.
In the above case ,the Directory mods_dir was getting reimported into main as it was declared as module mod mods_dir in its own .lib file.Removing mod mods_dir and directly importing its submodules solved the issue.
Module tree must be in the same layout like your file tree. You are defining module
crate::mods_dir::output_0
, and yet yoursrc/
directory containsoutput_0.rs
file, and notmods_dir/output_0.rs
. See a section of The Book on how to layout modules or The Rust Reference.You can solve it in two ways:
Fix your file structure/module structure (either put
output_0.rs
inmods_dir/
, or removemods_dir
module). This should be the best solution, as there is a common convention on how Rust projects are laid out, and if you change it, others might find it confusing.Use
#[path]
attribute on modules, to override where they point at.