Search code examples
rustrust-cargo

Use of undeclared crate or module in my project


I've been struggling for hours with an error in Rust and can't seem to resolve it.

Here's how my project is structured:

├── Cargo.toml
└── src/
    ├── main.rs
    └── controllers/
        └── user_controller.rs

In main.rs I have the following:

use controllers::user_controller;

However, when I try to run my project, I get the error message here: Error in terminal

Also in VS Code Error in VS Code

What am I doing wrong?

I tried already every possible solution, but nothing seems really to help


Solution

  • As user_controller.rs is nested in the controllers module, it is inaccessible to main.rs. Therefore, you need to make the user_controller module accessible.

    You can either:

    Create a controllers.rs file (controllers/mod.rs in legacy projects) in the src directory which contains the following:

    pub mod user_controller;
    

    This declares the public module user_controller. You can then use the module controllers and controllers::user_controller from main.rs.

    Alternatively, in main.rs, declare the module controllers and include the submodule user_controller :

    mod controllers {
        pub mod user_controller;
    }