# Why is Rust slow to compile, and what speeds it up?
Source: https://ruststudio.org/community/t/7/why-is-rust-slow-to-compile-and-what-speeds-it-up
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: cargo 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: cargo, performance, tooling | Views: 0

## Short answer
The Rust compiler does heavy checking and optimisation, and every dependency is compiled from source. Use cargo check while you edit, find the slow crates with cargo build --timings, drop unused dependencies and features, and share one build folder between projects so common dependencies are compiled once.

## Answer
## Answer
Compile time in Rust comes from three places: type checking and borrow checking, code generation (generic code is generated again for each type it is used with), and optimisation. On top of that, a first build compiles all your dependencies from source. Later builds are incremental, but a release build with link-time optimisation is slow by design.

You cannot change how much work the compiler does, but you can avoid doing it. The answers below are independent measures; the first is the one that changes your day-to-day most.

## Common mistakes
- Running a full `cargo build` to see whether the code compiles. `cargo check` stops before code generation and gives the same errors.
- Measuring compile time of the debug build and concluding that Rust is slow at everything. Debug builds skip optimisation on purpose.
- Turning on `lto` for development builds. Keep it in `[profile.release]`.

## Related questions
### Why does the first build take so long?
It compiles every dependency, including large ones such as an async runtime or a web framework, from source. Cargo keeps the results in `target`, so the second build is much faster until you run `cargo clean` or change the compiler version.

### Does adding more dependencies slow the build?
Yes, each one adds compile time and often more transitive dependencies. `cargo tree` shows what you pull in.

## Sources
- The Cargo Book, "Timings": https://doc.rust-lang.org/cargo/reference/timings.html
- The Cargo Book, "Environment variables" (`CARGO_TARGET_DIR`): https://doc.rust-lang.org/cargo/reference/environment-variables.html

## Answers (3)

### Answer 1 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 1: use cargo check while you edit
`cargo check` runs the front half of the compiler (parsing, type checking, borrow checking) and skips code generation and linking. It reports the same errors as a build and is the quickest feedback loop.

```sh
cargo check
```

Run `cargo build` or `cargo run` only when you need the program itself. Trade-off: it produces no executable, so it cannot catch link errors.

### Answer 2 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 2: find out what is slow, then remove it
`cargo build --timings` writes an HTML report to `target/cargo-timings/` that shows how long each crate took and which crates were built one after the other.

```sh
cargo build --timings
```

Use the report together with `cargo tree` to find a heavy dependency you do not need, or one where you enabled more features than you use. Turning off default features for a dependency (`default-features = false` in its entry in `Cargo.toml`) and enabling only what you call removes code from the build. Trade-off: it takes some reading, and a dependency's smaller feature set can disable something you use later.

### Answer 3 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 3: share one build folder between projects
Every project has its own `target` folder, so the same dependency at the same version is compiled again for each project. Setting one shared folder, with the `CARGO_TARGET_DIR` environment variable, lets Cargo reuse compiled dependencies between projects that use the same compiler and the same features.

```sh
# PowerShell
$env:CARGO_TARGET_DIR = "C:\cargo-target"
cargo build
```

```sh
# bash
CARGO_TARGET_DIR=$HOME/cargo-target cargo build
```

The shared folder also means one place to clean. Trade-offs: two builds running at the same time wait for each other because Cargo locks the folder, and the output of every project ends up in the same `debug` and `release` folders, so scripts that expect `target` next to the project need the new path.
