More than 1 file per module

Hello

I’m currently trying to learn Rust, and am having a bit of a fight with the module system.
My idea was to structure my project into different files because keeping everything in the same one gets ugly rather fast…
So I now have these files:

src
\--main.rs
|--definitions.rs
|--parsing.rs
|--evaluation.rs

Where definitions.rs just contains some enum declarations, parsing.rs and evaluation.rs both operate with the data structures defined in definitions, while main.rs uses stuff from all three of these files.

Now, how can I structure that in a way that works, preferably without having a folder for each new file?
I tried using

//main.rs
pub mod parsing;
pub mod evaluation;
pub mod definitions;

//parsing.rs and evaluation.rs
pub mod definitions;
//also tried use definitions;

However nothing I tried is really working. Is this even how I am supposed to structure my project when writing idiomatic Rust code?
Thank you for your help

//parsing.rs and evaluation.rs
use super::definitions;
// or
use crate::definitions;

This works because super looks up one module level, so if you are in parsing.rs, one level up is main.rs, and definitions is declared in main.rs.

With crate, it looks in the crate root, which is usually main.rs or lib.rs. Otherwise it is the same as super

2 Likes