Issue with Module Resolution in Rust Project Using Actix-Web"

I'm working on a Rust project using Actix-Web, and I'm facing issues with module resolution. My project has the following structure:

src/
  main.rs
  lib.rs
  routes.rs
  session.rs
  models/
    mod.rs
    issue.rs
  handlers/
    mod.rs
    issue.rs
  • In models/issue.rs, I define the Issue struct and related structures.
  • In handlers/issue.rs, I have functions like get_issue that use the Issue struct from models.
  • Both models/mod.rs and handlers/mod.rs correctly declare their respective submodules:
    • models/mod.rs contains pub mod issue;
    • handlers/mod.rs contains pub mod issue;

In routes.rs, I'm trying to import and use the get_issue function from handlers::issue using the following line:

use crate::handlers::issue::{get_issue};

However, I keep encountering unresolved import errors. I've ensured that lib.rs declares all necessary modules as follows:

pub mod handlers;
pub mod routes;
pub mod session;
pub mod models;

I've tried cleaning and rebuilding the project with cargo clean and cargo build, but the issue persists.

Even change use crate::handlers to crate::project_name_handler, nothing works

What might be causing these unresolved import errors? How can I ensure that the get_issue function and the Issue struct are properly recognized across my project?

My guess is that you have mod routes; in main.rs. The library and binary get compiled separately, with the binary using the library as a dependency, so the binary shouldn't redeclare the modules that the library already compiled. Instead, it should use them through the library: use project_name::routes;.

3 Likes

Thanks man! As we say here in Brazil, you are not a friend, you are a mayor.

1 Like

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.