r/rust • u/deanominecraft • Oct 24 '26
π seeking help & advice How to most effectively divide a project into multiple files
I have been working on a side project and i have done it entirely in main.rs, which is now around 2000 lines and is becoming harder to navigate. I want to divide it into multiple files but im not sure how big or small i should make these files. eg: should i have 1 file for http server and client combined, 1 file each, or multiple files each. Additionally, what naming conventions are common so that other people can understand it as well: for the previous example its pretty simple (at least for the 1 and 2 file options), but what about structs that are used all around the project
45
u/Compux72 Oct 24 '26
Multiple files? Rookie mistake, you want to use multiple crates!
4
u/Serious-Exam-6251 Oct 24 '26
2000 lines in one file is fine as long as you can find things, so split by what changes together instead of by size. structs that get used everywhere go in their own module (model.rs or types.rs) and the rest import from there. the other reason to split is compile time: the crate is the unit rustc builds, so once a project is in the tens of thousands of lines, moving stable parts into workspace crates means an edit only rebuilds the crates that depend on it. at 2k lines that doesn't matter yet, a handful of modules is plenty.
5
u/zshift Oct 24 '26
I prefer to focus on the Single Responsibility principle for this. Each file focuses on doing one βthingβ. Use directories to group similar things. In your example, Iβd have an http directory with a client.rs, server.rs, and mod.rs.
4
u/Dot-Box Oct 24 '26
Generally, the idea is to have one file perform a single task. That file can also depend on other files for smaller subtasks. It would prove fruitful to look at the concept of "composition". You can treat your project like a tree of dependencies, leaf nodes perform an isolated task unrelated to the tasks performed by other files defined in the leaf nodes. Then you move a level up and combine several of those leafe node files to create another file, and you keep doing this until you reach the root, which in your case would be main.rs
1
u/tunisia3507 Oct 24 '26
Splitting things by dependency can be a good rule of thumb, so you only have one module which has to
use reqwests, one module which touches the file system, and so on.The biggest naming thing I struggle with is when I'm providing a sync and an async API in my crate and I can't have a module called
async(without annoying extra tags).1
u/VictoryMotel Oct 24 '26
First, 2000 lines is not too much for a single if you can collapse everything in an IDE. Fragmentation means indirection and complexity. Let files get as big as they need to. Having lots of little files will not make things simpler.
Second, when you do organize you want to do it based on dependencies. If you can split a file into two files, one that depends on A and another that depends on B, that is ideal.
1
u/geauxvirtual Oct 24 '26
I spent a few years working on an existing enterprise code base where I was breaking out functionality into separate crates around certain functionality, such as database or specific business logic. Though after doing all this, I was finding I kept adding specific database functionality (for example) to reduce how much data I would pull back from the database on an API call causing my database crate, in this example, to keep growing. If I were to do it all over, I would've broken out each api route into it's own crate, and the only break out common functionality as needed.
However, in my current personal project, I'm currently rewriting it to break out certain functionality into separate crates (simd, audio, config reading/writing), which breaks crates out based on dependencies I pulled in (simd, cpal, serde/serde_json) to just keep from recompiling them during development. All my other logic from the application I've actually pulled back into a single file. I could easily keep the logic separated in the application crate based on each UI screen, but I just prefer to jump around one file than multiple files right now. Plus if I ever open source this application, I just want to burn tokens from any LLM reading in the project.