Rusts hidden talents speeding up builds and managing versions

These tips are building on top of rustup the official installer for the rust compiler and packet manager cargo.

Rust version or toolchain per project

The idea is to include the version of the toolchain used can be included in the project. Allowing the developers of a project to upgrade the toolchain together with a simple git pull. This feature was asked after people working on android projects came into contact with the gradle wrapper gradlew.

Setting a fixed version:

echo "nightly-2019-01-21" > rust-toolchain
echo "1.34.0" > rust-toolchain

Setting a named version:

echo "nightly" > rust-toolchain
echo "beta" > rust-toolchain
echo "stable" > rust-toolchain

Note: if you choose stable, beta or nightly your project members will still have to run rustup update manually.

Cleaning up

After some time your machine amounts many different versions.

To remove them first list them:

rustup toolchain list

Then remove them like this:

rustup toolchain uninstall 1.9.0
rustup toolchain uninstall 1.13.0-x86_64-unknown-linux-gnu
rustup toolchain uninstall beta

Global build cache

This helps saving build time and energy however you need to know three important things:

  1. You must stay on the same compiler version on your machine for (almost all projects)
  2. You can only build one project at the same time (one cargo command per user per machine)
  3. Building binaries or projects with the same name will result in the last compiled binary with that name to be executed

For setting this up you need to extend your shell's environment.

export PATH="$HOME/.cargo/bin:$PATH"
export CARGO_TARGET_DIR=~/.cargo/build_cache/

Overriding per project

For some projects you will not want to use the global cache. If you are already using .env you can unset the CARGO_TARGET_DIR variable there or do it manually:


You should document if you project breaks with a shared target directory or if your project hugely benefits from it.

Another approach is to use multiple shared target directories and group them by topic for example projects with actix.

Alternative: sccache

After publishing this on reddit I was pointed to sccache from Mozilla.

However their rust support is still marked expremimental. There are some caveats with env!, macros, target specs and files read at compile time.

Reading has it with rust 1.18 or later you can enable sccache with the following commands:

cargo install sccache
export RUSTC_WRAPPER=sccache