How to scale a rust project up or down

Scaling a Rust project is not one decision. It happens on three separate axes: how much code lives in one place, how many crates you pull in, and how big the output is. Beginners usually move all three at once — splitting files, adding a workspace and adopting an async runtime in the same week — and then cannot tell which change caused the three-minute build. Move one axis at a time.

Measure three numbers before you change anything

Run these from the project root and write the results down:

  • cargo build from a clean target/ directory, timed with time.
  • cargo tree --edges normal | wc -l for the size of the dependency tree.
  • ls -lh target/release/<binary> after cargo build --release.

Reasonable baselines for a beginner project on a four-core laptop: a command-line tool using clap and serde lands at roughly 60–90 crates in the tree, a 20–40 second clean debug build, and a 2–3 MB binary. A project under about 1,000 lines in fewer than ten files is not a scaling problem yet, whatever it feels like. Re-measure after each change; if a number did not improve, revert.

Scaling up: split files before you split crates

The cheapest way to grow is to cut a sprawling file into modules inside the same crate. Treat 400–600 lines as the point to start looking, and 800 as the point to act. A single crate holding 5,000–10,000 lines is normal Rust and still compiles quickly, so there is no rush to go further.

Turn src/parser.rs into src/parser/mod.rs plus src/parser/lexer.rs and src/parser/ast.rs, and keep everything private by default. Widen to pub(crate), then pub, only when the compiler or your tests demand it. Splitting a file costs seconds of build time and no design commitment, which is what you want while the shape of the code is still moving.

Scaling up: promote a module to its own crate when it earns it

Three signals justify a separate crate: you want to run its tests without building the rest, you have more than one binary sharing the code, or a clean build has passed about 60 seconds and most of that is code you did not touch.

Then create a workspace with a root Cargo.toml:

[workspace]
members = ["app", "core", "cli"]

Keep a hobby workspace to three to eight members. Each member adds a small fixed overhead — expect 5–10% on a full clean build — and pays you back with per-crate rebuilds. Dependencies must point one way, because Cargo forbids cycles; draw the arrow before you create the folder.

Scaling up: use cargo features instead of maintaining forks

When a second use case appears, resist copying the project. Add a feature instead:

[features]
default = ["json"]
json = ["dep:serde_json"]

Keep the total at four or five features at most, and add two commands to your checks: cargo build --no-default-features and cargo build --all-features. Features are additive and unify across the whole dependency graph, so every feature must compile alongside every other one. If you are testing eight combinations, you have more features than a beginner project needs.

Scaling down: cut dependencies before you cut code

Deleting your own code rarely shrinks a project as much as deleting someone else's. Ask cargo tree -i <crate-name> who pulled a crate in, and cargo tree -d which crates appear twice at different versions — duplicates are a common reason a tree quietly doubles.

The biggest single saving for small tools: a program making one HTTP request does not need an async runtime. Swapping a full async stack for a blocking client typically removes 30–50 crates and 10–20 seconds from a clean build. Also set default-features = false on any dependency that offers it and add back only what you call; this is routine for serde, regex and reqwest.

Scaling down: reduce type-level cleverness

Generics compile once per concrete type: runtime speed paid for at build time. If a one-line change takes more than 5 seconds to rebuild, or a few thousand lines take a minute, look for generic functions called with many types, deep trait bounds and heavy macro use.

The usual downscale is Box<dyn Trait> at the boundary where types vary — one compiled copy instead of N — and impl Trait in argument position instead of a named parameter. Keep generics where they are hot and used with few types. Async is the other heavy lever: a program juggling a handful of connections is often fine with ordinary threads, and dropping tokio from a small project removes both crates and a whole mental model.

Scaling down: collapse crates and shrink the release profile

If the workspace has reached a dozen members and you are the only contributor, merge them back: move the folders under src/ and replace path dependencies with mod declarations. Do it on a branch, run the tests, merge when green. Three crates that always change together are one crate.

Then set a release profile in the root Cargo.toml:

[profile.release]
lto = true
codegen-units = 1
strip = true
opt-level = "s"

Expect roughly 30–50% off the binary size and 1.5–2× longer release builds. Skip panic = "abort" if you rely on catch_unwind or run tests that expect unwinding.

When the project fights back: symptoms, causes, fixes

A clean build takes more than three minutes. Likely cause: a large dependency tree, or a proc-macro-heavy crate such as a full web framework. Fix: run cargo tree --edges normal | wc -l; if it is over 200, remove the largest optional dependency before touching your own code.

Changing one line rebuilds dozens of crates. Cause: a shared crate low in the workspace whose public types everything depends on, or a build script that reruns every time. Fix: keep leaf crates small and stable and push churn upwards.

The release binary is unexpectedly large. Cause: leftover debug symbols, an async runtime, or a statically linked TLS stack. Fix: add strip = true and lto = true, then run cargo bloat --release -n 20 to see what dominates.

Adding one dependency breaks the build. Cause: a duplicate version conflict, or a feature combination nobody compiles. Fix: cargo tree -d, then pin or replace the offending crate.

FAQ

Is a single crate with 5,000 lines a problem?

No. One crate that size builds fast and is easier to navigate than four crates with circular ambitions. Split at the file level and revisit only when build times or a second binary force it.

Does scaling up always mean a workspace?

No. Files, modules and features cover most growth up to a few thousand lines. A workspace earns its overhead when you have a second binary, a piece worth testing alone, or a clean build past about 60 seconds.

Can I scale back down after splitting into crates?

Yes, and it is usually cheap: merge the folders into src/, replace path dependencies with mod lines, and fix the use paths the compiler flags. Do it on a branch with a green test suite and the merge is mechanical.