Can't the files of two folders in src directory reference each other

This is my directory structure

covid_19
  |__src
       |__api
       |    |__user_api.rs
       |    |__mod.rs
       |__common
       |    |__response.rs
       |    |__mod.rs
       |__domain
            |__user.rs
            |__mod.rs
     

I want to quote user.rs file and response.rs struct in user_api.rs file

user_api.rs:

use crate::common::response;
use crate::domain::user;

use actix_web :: {
    post,
    web,
    Result,
    Error,
    HttpResponse,
    Responder,
};

error message:

error[E0432]: unresolved import `crate::common`
 --> web/covid_19/src/api/user_api.rs:1:12
  |
1 | use crate::common::response;
  |            ^^^^^^ could not find `common` in the crate root

error[E0432]: unresolved import `crate::domain`
 --> web/covid_19/src/api/user_api.rs:2:12
  |
2 | use crate::domain::user;
  |            ^^^^^^ could not find `domain` in the crate root

Is there any solution? It seems that rust does not support this?

Of course Rust supports this. Do you not have a lib.rs or main.rs file? Can you show us your mod statements?

hahah, I just solved it. I'm in main.rs add

mod domain;
mod common;

But I'm very curious about why adding in lib.rs file doesn't take effect, but adding in main.rs file takes effect

lib.rs and main.rs I have both file

The lib.rs and main.rs files define two separate crates. You can access the lib.rs crate from the main.rs crate with whatever package name you have specified in your Cargo.toml instead of crate::. Other files are in the crate that mentions them with a mod statement. (Don't mention it with a mod statement in both — that will result in the file being compiled twice.)

In other words, if you add crates in lib.rs, don't add them in main.rs file, right?

modules, but yes

Thank you for your prompt reply :slight_smile:

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.