How to import the same file into two different modules without duplicating it

So this is a thing I get confused about a lot.

I have a struct in a file and I want to import that struct into two different modules?

My file structure looks like this.
Player.rs
Mod1.rs
Mod2.rs
Main.rs

I want to have access to Player.rs from both Mod1 and Mod2? How do I do this? Seems it should be simple but rust seems to want me to put two copies of the same file into subdirectories for each module... but I just want it to point to the existing file that has the struct.

Am I missing something, I can't find any documentation on how to do this.

In main.rs, which is the root of your module tree, you can create the three submodules:

// main.rs
mod player;
mod mod1;
mod mod2;

And then in any of the submodules, you can use the crate:: prefix to refer to other modules by their paths starting from the root. For example:

// mod1.rs
use crate::player::MyPlayerType;

Ah that worked! Thanks!

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.