'Significance of import statement inside and outside of an module

I am very new to Rust and trying my best to understand what is going on. Directory structure looks like this.

├── Cargo.lock
├── Cargo.toml
├── src
│   ├── main.rs
│   ├── models.rs
│   ├── routes.rs
│   └── utils.rs

Error is in routes.rs file.

use crate::models::api_models::Response;
pub mod api_paths {
    pub fn home() -> (String, String) {
        println!("In Home Page");
        return ("HTTP/1.1 200 OK".to_string(), Response {
            code: 200,
            message: String::from("In Home Page"),
        }.to_string())
    }
    pub fn not_found() -> (String, String){
        println!("This path is not implemented");
        return ("HTTP/1.1 404 Not Found".to_string(), Response {
            code: 200,
            message: String::from("404 Not Found"),
        }.to_string())
    }
}

Compiler is saying.

error[E0422]: cannot find struct, variant or union type `Response` in this scope
 --> src/routes.rs:6:48
  |
6 |         return ("HTTP/1.1 200 OK".to_string(), Response {
  |                                                ^^^^^^^^ not found in this scope
  |
help: consider importing this struct
  |
4 |     use crate::routes::Response;
  |

error[E0422]: cannot find struct, variant or union type `Response` in this scope
  --> src/routes.rs:13:55
   |
13 |         return ("HTTP/1.1 404 Not Found".to_string(), Response {
   |                                                       ^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
4  |     use crate::routes::Response;
   |

warning: unused import: `crate::models::api_models::Response`
 --> src/routes.rs:1:5
  |
1 | use crate::models::api_models::Response;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0422`.
warning: `web-app` (bin "web-app") generated 1 warning
error: could not compile `web-app` due to 2 previous errors; 1 warning emitted

But, if I put the import statement inside the mod, then it works just fine.

pub mod api_paths {
    // This works!
    use crate::models::api_models::Response;
    pub fn home() -> (String, String) {
        println!("In Home Page");
        return ("HTTP/1.1 200 OK".to_string(), Response {
            code: 200,
            message: String::from("In Home Page"),
        }.to_string())
    }
    pub fn not_found() -> (String, String){
        println!("This path is not implemented");
        return ("HTTP/1.1 404 Not Found".to_string(), Response {
            code: 200,
            message: String::from("404 Not Found"),
        }.to_string())
    }
}

My models.rs looks like this.

use std::{
    fmt
};
pub mod api_models {
    pub struct Response {
        pub code: i16,
        pub message: String,
    }
    
}
impl fmt::Display for api_models::Response {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{{\"code\":\"{}\", \"message\":\"{}\"}}",
            self.code, self.message
        )
    }
}

I am not able to understand why. Please suggest.



Solution 1:[1]

your full answer is here

but for quick solution if you have a project structure like this

- src/main.rs
- src/models.rs
- src/utils.rs
...

you must define what modules your project have so for that you just need add the following in main.rs

mod models;
mod utils;

above code tell compiler what modules you have and remember main.rs it's your main module so you must define modules there

if you have lib crate, lib.rs will be your main module

now for import data types you have two options

first from root crate like this

use crate::models::*; // import every things in models module

second from parent or super crate, it's uses when inner module wanna access super module data types

use super::*

remember use keyword must place inside the module

if you have routes.rs and then create another module '''api_path``` inside it

routes is super module and second one will be inner module and must have another import codes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1