FAQ - Help and questions

Why is Rust slow to compile, and what speeds it up?

#cargo#performance#toolingpublished 3 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail
Last post

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.

Checked with cargo 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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].

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    3 answers from the Ruststudio team: different approaches, each with its own trade-offs

    Sort: Oldest Newest
    #2

    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.

      QuoteReply
      Share
      Hacker NewsRedditLinkedInE-mail
      #3

      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.

        QuoteReply
        Share
        Hacker NewsRedditLinkedInE-mail
        #4

        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.

          QuoteReply
          Share
          Hacker NewsRedditLinkedInE-mail