Rust Toolchain: A Complete Guide to Managing and Configuring Your Rust Environment
The Rust toolchain is a collection of tools and components that streamline the development, building, and maintenance of Rust projects.
π In this article, weβll explore how to use rustup to manage Rust compiler versions and components, understand the different release channels, configure build profiles, and apply overrides for specific projects.
This practical guide will help you quickly master the essentials of the Rust toolchain and tailor it to your development needs.
βββ 1. rustup
For those who want to start using Rust in their projects, please start here.
- the basic Rust setup is managed by the
rustup
utility.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This will install rustup
, rustc
, cargo
and configure $PATH
.
By default, the host platform targets are installed (e.g. x86_64-pc-windows-msvc
)
- To get a list of installed toolchains and check the active one
> rustup toolchain list
stable-x86_64-pc-windows-msvc (active, default)
1.74.0-x86_64-pc-windows-msvc
- Or a command that will display additional information about installed tagrets.
> rustup show
installed toolchains
--------------------
stable-x86_64-pc-windows-msvc (active, default)
1.74.0-x86_64-pc-windows-msvc
active toolchain
----------------
name: stable-x86_64-pc-windows-msvc
active because: it's the default toolchain
installed targets:
thumbv6m-none-eabi
wasm32-unknown-unknown
x86_64-pc-windows-msvc
- To find the path to the toolchain
> rustup which rustc
C:\..\stable-x86_64-pc-windows-msvc\bin\rustc.exe
βββ 2. Channels
-
Rust is developed in 3 parallel branches
- stable β the most reliable, updated every
6 weeks
- beta β a βpreviewβ of the next stable, updated every 6 weeks
- nightly β daily builds, including the latest (but unstable) features
- custom β custom versions of Rust
- stable β the most reliable, updated every
-
To install any of the channels
rustup toolchain install stable
rustup toolchain install beta
rustup toolchain install nightly
- To update the corresponding toolchains
rustup update stable
rustup update beta
rustup update nightly
- To install the exact version
rustup install 1.74.0-x86_64-pc-windows-msvc
βββ 3. Override & Profiling
- Set
default
toolchain for the system
rustup default nightly
- To switch the toolchain for a specific project
cd project-dir
rustup override set nightly
- Custom profiles
In~/.cargo/config.toml
you can set profiles:
[profile.dev]
debug = true
incremental = true
[profile.release]
opt-level = 3
debug = false
lto = "fat"
panic = "abort"
βββ 4. Components
- Each installed toolchain can be upgraded with
components
rustup component add clippy fmt rust-src rust-analyzer
rustup component add llvm-tools-preview
rustup component add miri
- To install a component in another toolchain
rustup +nightly component add rustfmt
- Check all installed components on the active toolchain
rustup component list
- Remove component from active toolchain
rustup component remove clippy
βββ 5. Best Practice
- To ensure that the program runs identically on different machines, it is recommended to use the
rust-toolchain.toml
file.
[toolchain]
channel = "1.70.0" # 1.70.0 v on stable channel
components = ["rust-src", "clippy", "rustfmt"]
targets = ["thumbv6m-none-eabi"]
[profile.release]
opt-level = "z" # size optimization
lto = true # link-time optimization
Next time cargo build
will automatically install the required toolchain, components and targets on your machine.