Structure and access mods in sub folders

Hi,

I'm new to Rust, and learning how to structure my code. I want to create a folder structure as below:

.
|-- main.rs
`-- routes FOLDER
     |-- mod.rs
     |-- health_check.rs
     |-- main_screen FOLDER
     |    |-- last_updated.rs
     |    `-- mod.rs
     `-- secondary_screen FOLDER
          |-- last_updated.rs
          `-- mod.rs

I access main_screen/last_updated from main.rs like this:

use crate::routes::last_updated;

But I can't access secondary_screen/last_updated as I am unable to specify that I want the last_updated function in the secondary_screen mod.

How would one go about accessing functions from specific mods?

Thank you,
Ajit

What’s in your various mod.rs files? That’s what controls how the other files are imported.

In routes FOLDER mod.rs

mod health_check;
mod main_screen;
mod secondary_screen;

pub use health_check::;
pub use main_screen::
;
pub use secondary_screen::*;

In main_screen FOLDER mod.rs, and secondary_screen FOLDER mod.rs:

mod last_updated;

pub use last_updated::*;

Thank you.

You can say crate::routes::secondary_screen::last_updated.

(This might require changing mod secondary_screen; to pub mod secondary_screen;).

Thank you. Didn't realise that we had to put pub in front of the mod too!

Due to both mods having the same function name, I had to import the secondary_screen one as:

use crate::routes::secondary_screen::last_updated as secondary_screen_last_updated;

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.