Skip to main content

Should a .rs file have a size limit?

Question: What do existing Rust best-practice sources say about how large a single .rs file should be — and does any of it mechanically guard against a 37,799-line file? Date: 2026-12-06

TL;DR​

No primary Rust source sets a maximum file size. The only mechanical size rule in the ecosystem is clippy::too_many_lines — 100 code lines per function, in the pedantic group, allow-by-default — and DropTerm never enables it. The Book's testing chapter prescribes inline #[cfg(test)] mod tests in every source file, which is where most of DropTerm's bulk sits. File-level size has no upstream guard at all: app_shell.rs at 37,799 lines breaks no Rust rule, because no Rust rule exists.

Findings​

1. No official Rust source caps file or module size​

The Rust Style Guide (rust-lang/rust, src/doc/style-guide/src) has nine chapters: SUMMARY, advice, cargo, editions, expressions, items, nightly, principles, statements, types. Searching advice.md, principles.md, and items.md for file-size, module-size, or splitting guidance returns nothing. The Rust API Guidelines checklist has no guideline about physical source length or module size; its structural items concern API shape (private fields, newtypes), not file organization. This is rule absence, not an unenforced rule.

2. The one mechanical rule is per function, and off by default​

Clippy's too_many_lines is the only size lint in the compiler toolchain.

  • Default threshold, clippy_config/src/conf.rs: too_many_lines_threshold("too-many-lines-threshold"): u64 = 100, documented as "The maximum number of lines a function or method can have".
  • Group, clippy_lints/src/functions/mod.rs: pub TOO_MANY_LINES, pedantic, "functions with too many lines".
  • Scope, clippy_lints/src/functions/too_many_lines.rs: it measures the function body span, skips closures ("Closures must be contained in a parent body, which will be checked"), and counts only lines containing code outside comments.

Because the group is pedantic (allow-by-default), and DropTerm's just clippy is cargo clippy --all-targets ... -- -D warnings over default and warn lints only, the lint has never fired here. There is no too_many_lines for files, and no rustfmt option that limits total file lines — Configurations.md exposes only per-line widths such as max_width.

3. The module lints control layout, not size​

Clippy's module_style.rs declares MOD_MODULE_FILES and SELF_NAMED_MODULE_FILES, both restriction:

"Checks that module layout does not use inline modules. Inline test modules (anything annotated with #[cfg(test)]) are not linted."

So the one lint that could push code out of a file explicitly exempts test modules. Nothing in the lint set caps a file's length.

4. Tests are supposed to be inline, which is where the bytes are​

The Book, "Test Organization"

"You'll put unit tests in the src directory in each file with the code that they're testing. The convention is to create a module named tests in each file to contain the test functions and to annotate the module with cfg(test)."

That convention is enforced in the same direction by clippy::tests_outside_test_module (added 1.70.0, restriction): test functions must be inside a testing module, and items_after_test_module keeps that module last. Both push tests into the source file and neither limits its size. A file-heavy codebase is the straightforward outcome of following the Book.

5. The books with a reputation for authority say nothing​

  • Effective Rust (David Drysdale) lists 35 items across types, traits, concepts, dependencies, tooling, and beyond-standard-Rust. None addresses module size, file size, or splitting source files. Closest: Item 22 "Minimize visibility" and Item 25 "Manage your dependency graph" — neither is about file sizing.
  • Rust Design Patterns (rust-unofficial/patterns): the landing page and the idioms index give no size guidance; the nearest statement is "Keep It Simple, Stupid" about unnecessary complexity.

What DropTerm has today​

Measured 2026-12-06 over dropterm/ (109 .rs files, 200,691 lines; carried/ excluded as vendored):

  • 48 files are ≥1,000 lines — 44% of the crate.
  • 22 files are ≥2,000 lines; eight ≥5,000; two ≥10,000.
  • app_shell.rs is 37,799 lines: 12,464 production, 25,335 inline tests.
  • 49 functions exceed clippy's 100-code-line threshold; the worst are lua_host.rs:2414 install_port (656), engine.rs:4087 update (425), and terminal/view.rs:2602 paint_pane (324).
  • No [lints] table in DropTerm's own manifests (the only one in the tree is vendored carried/gpui-component/Cargo.toml), and no clippy.toml, so the only mechanical size rule in Rust is switched off for this crate.

Scoring​

Four dimensions, each graded on the same A–F ladder, and a file takes the worst grade among them. Thresholds are stated here so the score can be recomputed; two are project rules, one is the official clippy rule.

DimensionABCDFAuthority
Total lines<1,0001–2k2–5k5–10k≥10kinternal review signal (code-review-and-quality)
Inline mod tests lines≤500501–2k2–5k5–10k≥10kproject rule (AGENTS.md)
Longest function, code lines≤100101–199200–299300–499≥500clippy::too_many_lines (primary)
FileTotalProdTestLongest fnGradeDeciding dimension
src/app_shell.rs37,79912,46425,335293Ftotal + inline tests
src/core/lua_host.rs17,1445,50011,644656Ftotal + function
src/panels.rs7,2119896,222112Dinline tests
src/core/terminal/engine.rs6,0796,0790425Dtotal + function
src/core/terminal/suggest/tests.rs5,87305,873130Dtotal
src/core/plugins_manifest.rs5,7001,9643,736181Dtotal
src/core/settings/tests.rs5,35305,353102Dtotal
src/settings.rs5,0524,437615129Dtotal
src/terminal/view/tests.rs4,92204,922218Cfunction
src/core/slots.rs4,5601,2433,317122Ctotal
src/terminal/view.rs3,9843,9840324Dfunction
src/core/settings.rs3,8673,8670109Ctotal
src/tab_bar.rs2,5182,5180211Cfunction
src/core/terminal/engine/tests.rs7,45607,45677Dtotal
src/core/terminal/suggest.rs3,1803,180071Ctotal
src/core/plugins_install.rs2,8711,2711,60074Ctotal
src/tab_bar/tests.rs2,84902,84985Ctotal
src/core/config/tests.rs2,67402,67477Ctotal
src/band.rs2,1761,1401,03697Ctotal
src/main.rs2,0737161,35788Ctotal
src/executor/layer_shell.rs2,0589371,12163Ctotal
src/terminal/suggest.rs2,0118661,145100Ctotal

Distribution: 2 F, 7 D, 13 C, none A or B. Every graded file fails the project's 1,000-line signal and most fail the 500-line test-module rule; 49 functions fail the only official rule. The score is not "how close to a Rust standard" — no such standard exists — it is "how far past the two project signals, with the official function limit as the one hard line".

Recommendation​

There is nothing upstream to lean on, so the guard has to be ours and mechanical, because the current gate is all-green or all-red and neither state ever mentions size.

  1. Turn on the one official rule. Add too_many_lines = "warn" to [lints.clippy] (default 100, or raise the threshold in clippy.toml to 200 as a first ratchet). Today the repo has no lints table, so the rustc/clippy gate cannot see function size at all. This is a decision for the owner because it will surface 49 findings at once.
  2. Make the 500-line test rule a check, not a paragraph. It currently lives only in AGENTS.md and is violated in 11 files. A recipe comparing an inline mod tests length against 500 fails loudly and cannot go stale.
  3. Ratchet the file list. Add a checked-in allowlist of the 48 files ≥1,000 lines with their current sizes; fail when a listed file grows and when a new file crosses 1,000. That is the only shape that makes a size rule survive an all-green commit stream, because the alternative (too_many_lines on functions only) never looks at whole files.

Rejected alternatives​

  • Lean on the review skill's 1,000-line file signal. That is the current state and it failed: the signal is advisory, it is only read by /review and /ship, and the commits that grew app_shell.rs from 4,400 to 37,799 lines were small fixes that never ran either.
  • Enable the whole pedantic group. It would surface the 49 size findings plus every other pedantic lint, burying the size signal in unrelated noise.
  • Split files to a numeric target. No primary source supports a target size, and splitting app_shell.rs by line count would produce arbitrary modules; the real work is naming boundaries, which is design, not a lint fix.

Sources​

Unverified​

  • The "around 1,000 lines per file" figure has no primary source. It appears only as an internal heuristic in this repo's code-review-and-quality skill; no Rust-language or tooling document endorses it.
  • rustfmt was checked by its option headings, not by an exhaustive semantic audit of every option; the conclusion "no file-size option" is from that listing.
  • Rust for Rustaceans was not surveyed: it has no public primary text to quote, and secondary summaries were deliberately excluded.
  • The pattern book was checked at its landing page and idioms index only; a size recommendation buried in a single pattern page is not ruled out.