Sub topic 11
Updating Rust and Choosing a Release Channel
This chapter assumes you have installed Rust with rustup and can run rustc --version and cargo --version. You should also be comfortable running commands in a terminal. Here you will learn how to keep Rust current and how to pick which version of Rust a project uses.
How Rust releases work
Rust ships on a fixed schedule. A new stable version comes out every six weeks. Rust moves through three release channels on the way to a stable release:
- stable is the release most people use. It changes every six weeks and is the default after a rustup install.
- beta is the next stable release, available for testing. It is promoted to stable after about six weeks.
- nightly is built every night from the latest development code. It is the only channel that allows experimental features, which are marked with
#![feature(...)].
A toolchain is one complete set of tools for a channel: rustc, Cargo, the standard library, and related tools. rustup can hold several toolchains side by side and switch between them.
Updating to the latest release
Run this command to update every installed toolchain, and rustup itself:
rustup updateRun rustup update before you start a new project, or when a crate asks for a newer Rust than you have. Some crates declare a minimum supported Rust version. If yours is older, Cargo will report an error that names the version it needs.
To see what you have installed, list the toolchains:
rustup toolchain listThe output shows each toolchain name, such as stable-x86_64-unknown-linux-gnu. The toolchain in use is marked (default), or (active) when a directory override applies.
Installing other channels
To install beta or nightly next to stable, name the channel:
rustup toolchain install beta
rustup toolchain install nightlyInstalling a channel does not change your default. Your commands keep using stable until you choose otherwise.
Choosing which toolchain runs
You can pick a toolchain in three ways, from narrowest to widest scope.
- For one command. Put
+channelright after the command name. This works withcargoandrustcwhen they are run through rustup. - For one directory. Use
rustup override set. It applies to that directory and its subdirectories. - As the default. Use
rustup default. It applies everywhere that has no override.
# One command only
cargo +nightly build
# Everything inside the current directory
rustup override set beta
# Remove that override again
rustup override unset
# Change the default for the whole machine
rustup default stableUse rustup show to see which toolchain is active in the current directory and why. This is the first command to run when a build behaves differently than you expect.
Pinning a toolchain for a project
An override set with rustup override set is stored on your machine, so teammates do not get it. To share the choice, add a rust-toolchain.toml file at the project root:
[toolchain]
channel = "1.80.0"
components = ["clippy", "rustfmt"]The channel value can be stable, beta, nightly, or an exact version number. When anyone runs cargo in this project, rustup installs that toolchain if it is missing and uses it. The components list also installs Clippy and rustfmt, which later chapters cover.
Pin an exact version when you need identical builds across machines, such as in a team or on a CI server. Use stable when you want to follow new releases.
Common errors
rustup: command not found. The~/.cargo/bindirectory is not on yourPATH. Open a new terminal, or fixPATHas described in the troubleshooting chapter.error: the 'cargo' binary ... is not installed for the toolchain. The toolchain you selected is missing or incomplete. Runrustup toolchain install <name>.error[E0554]: #![feature] may not be used on the stable release channel. The code uses an experimental feature. Build it withcargo +nightly build, or remove the feature.- A build works on your machine but fails on a colleague's. You may be on different versions. Compare the output of
rustup show, then pin a version inrust-toolchain.toml.
Exercises
Exercise
You want to try one project with the nightly compiler, but every other project on your machine should keep using stable. Which two commands or approaches let you do this without changing your default toolchain?
Show answer
Run cargo +nightly build (or another command with +nightly) for a single command, or run rustup override set nightly inside that project's directory. Both leave the default unchanged. You must first install nightly with rustup toolchain install nightly if it is missing.
Exercise
A teammate's build fails because they have an older Rust version than you. What file would you add to the project so that everyone uses the same toolchain, and what does the channel field accept?
Show answer
Add a rust-toolchain.toml file at the project root. The channel field accepts stable, beta, nightly, or an exact version number such as 1.80.0.