Mathias Hemmer
Mathias Hemmer

Reputation: 87

How do I import a Rust file in a nested folder?

I'm trying to separate my files for better project management:

src/   
|-main.rs   
|-entities/   
|--paddle.rs   

Now, I'm trying to import my paddle.rs in my main.rs: paddle.rs:

fn setup(){
  
}

How should I import it? I tried use, mod... none seems to work. I've read the Rust book but can't figure out how it's supposed to work!

Upvotes: 1

Views: 639

Answers (1)

aedm
aedm

Reputation: 6564

You need to add a mod.rs file to your entities folder with the following content:

pub mod paddle;

Make sure your setup function in paddle.rs is public:

pub fn setup() {...}  

Then you can use your new module in main.rs:

mod entities;

fn main() {
  entities::paddle::setup();
}

Find more here.

Upvotes: 2

Related Questions