skip to Main Content

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. –

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    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.

    mods_dir.lib
    
    PREVIOUS:
    mod mods_dir {
        pub mod output_0;
    }
    
    
    NOW:
     pub mod output_0;
    

  2. Module tree must be in the same layout like your file tree. You are defining module crate::mods_dir::output_0, and yet your src/ directory contains output_0.rs file, and not mods_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:

    1. Fix your file structure/module structure (either put output_0.rs in mods_dir/, or remove mods_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.

    2. Use #[path] attribute on modules, to override where they point at.

    #[path = "."]
    mod mods_dir {
        pub mod output_0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search