How to write module?


In C++ my projects have many many files. Each class in its own file. How should I write module in Rust?
Do I need write all functionality in one module? Is it good to place each class in its own file in Rust?


Well, Rust doesn’t have classes, exactly, but one struct per module is a bit excessive, I think.

The usual suggestion is “group similar stuff together”, but this is kind of a fuzzy answer. This sort of thing is very subjective.


If you give a simple testcase similar to what you’re actually trying to do people will be able to give you a more specific answer. As it is, this is pretty vague and hard to give a better answer than look at this page.


My usual practice narrows down to two kinds of ‘modules’.

The first one is a single unit of functionality, like a single wrapper type for something. The main aim is to have it in single place, isolated, with well-defined interface.

The second one is a “gathering of functions”, where I have many functions, or structures, which either have something in common, or serve similar purpose.

Anyway, my usual practical merit is “keep under 500 LOC”, which is subjective.