Structure and access mods in sub folders


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

`-- routes FOLDER
     |-- main_screen FOLDER
     |    |--
     |    `--
     `-- secondary_screen FOLDER

I access main_screen/last_updated from 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,

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

In routes FOLDER

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, and secondary_screen FOLDER

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.