How can I plug modules from src/modules
into main.rs
You can do:
#[path = "modules/module_1.rs"]
mod module_1;
In Rust, modules are a hierarchy, and directories and files are expected to match that hierarchy rather than having arbitrary separate organization. This means that if you currently have the files
src/main.rs
src/modules/foo.rs
then standard practice is to either move src/modules/foo.rs
to src/foo.rs
(placing it to be a child of the crate root main.rs
), or create the intermediate module file src/modules.rs
[1] which contains mod foo;
, and put mod modules;
in src/main.rs
.
While you can override the directory structure using the #[path]
attribute, you should not. Rust projects following the built-in code organization conventions is important for readability â it allows any Rust programmer to easily navigate an unfamiliar project.
or
src/modules/mod.rs
, equivalently âŠī¸