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
This feature was asked after people working on android projects came into contact with the gradle wrapper
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
nightly your project members will still have to run
rustup update manually.
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:
- You must stay on the same compiler version on your machine for (almost all projects)
- You can only build one project at the same time (one cargo command per user per machine)
- 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.
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