Hello world ! Is there a way in Rust to include the content of another .rs file and compile it as a unique file ? I mean splitting code into several files without using the module system.
You can, but that's an unusual requirement. Do you have a reason to avoid modules? I know they can be a bit confusing at first, but generally that's the proper way to split Rust projects into multiple files.
I'm not avoiding modules. When a module is too long, I would like to be able to split it and include the other files without having to call functions and pass params.
It's more usual to do that with module re-exports. For example, a large module might contain
mod part_1;
pub use part_1::*;
mod part_2;
pub use part_2::*;
#[cfg(test)]
mod tests;
and then part_1
and part_2
's items are visible outside in the same way as if they were inline. An advantage of this over include!
is that part_1
and part_2
can have items and use
s that are private from each other. (I find that it is frequently confusing for multiple files to share use
s.)
This is typically done by reorganizing the code into multiple, smaller modules. Also note that Rust allows multiple impl
blocks for the same type, so you can break down one huge impl Foo {}
into multiple impl blocks in different (sub)modules.
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.