New rust user here. I'm just learning, and am coming late to this thread. Like @motoras, I also am "surprised" at the requirement that a rust module fit in a single source file.
Let's take Java as a counter-example.
My mental model roughly equates a rust module with a java package. Multiple rust modules can be bundled into a crate, and multiple java packages can be bundled into a jar. In both languages many different constructs (structures, classes, constants, etc.) can be defined in the scope of that rust module / java package.
In general, java chose to map a package to a file system directory. I can have many .java files in the directory, and they together form that package's compilation unit. I can put my java code in a single file, and when it gets too large for my own taste or organization best practices, I can split it into separate files. I could organize the files by class, or maybe split my documentation in a separate file, whatever. Subpackages become subdirectories, and themselves can comprise multiple .java files.
Let's take Go as a counter-example.
My mental model roughly equates a rust module with a go package. I can likewise split my package implementation across multiple .go files in the same directory. I can likewise bundle multiple go packages and subpackages into a go module for distribution and sharing akin to a crate.
Rust has chosen to map a module to a file system file, full stop. I have the choice of putting the code of module foo
into the file foo.rs
, or I can create subdirectory foo
and put the source into foo/mod.rs
. But it's a single source file. That is a choice made by the language, so with respect to @mrcnski, I believe it is a language-level question.
Rust gives me the ability to re-export identifiers so that they appear to "live" in a different module. Thus I can choose to break my implementation up into multiple submodules, and "re-assemble" them by re-export.
That being said, as a rust newbie coming from java and go that just feels... odd. As in, it feels odd to be constrained that way, when file system subdirectories could have been used as in other languages to let me as a developer choose to split my module code into different source files without having to spill over module boundaries.
This won't be a perfect analogy: I write a new language that requires any function definition to be in a single source file line. When I'm asked, "why don't you let people decide where to put their line breaks to organize their code?" I can't understand why they care about 4K line length in their source. If they don't like it, they can split their function into multiple subfunctions, one per line, and simply call them from the top function... problem solved! I know... it's not a perfect analogy.
Bottom line: This feels to me that a singe source file per rust module is an odd language design decision. However, it's the choice that was made; that's the way it works, and if I want to use the language I'll figure out the idiomatic ways to organize my code with the contstraints I'm given.