skip to content

In a Rust repository, what does a `rust-toolchain.toml` file pin, and what can override it for a single command?

level: middleimportance: should knowfreq 40%

answer

  1. a [toolchain] table
  2. channel, components, targets, profile
  3. found by walking up directories
  4. plus sign beats the file

basics

~20 s

A rust-toolchain.toml makes rustup use a named toolchain, such as 1.99.1 or a dated nightly, plus listed components and targets, for every command run in that directory tree. A +toolchain argument or RUSTUP_TOOLCHAIN overrides it.

solid answer

~40 s

`rust-toolchain.toml` is read by rustup's `cargo` and `rustc` proxies, which look for it by walking up from the current directory. Its mandatory `[toolchain]` table sets `channel` (`stable`, `beta`, `nightly`, a version such as `1.99.1`, or a dated nightly such as `nightly-2026-10-25`) or a custom `path`, plus additive `components` and `targets` and an install `profile`. Precedence runs: `cargo +toolchain` shorthand, then `RUSTUP_TOOLCHAIN`, then a `rustup override` directory override or the file, whichever is closer, then the default toolchain. Pinning a version makes CI and every developer use the same compiler, and the project bumps it by pull request. It is not Cargo's `rust-version`, which only states the oldest toolchain a package supports.

code

toml · 5 lines
toml
[toolchain]
channel = "1.99.1"
components = ["clippy", "rustfmt"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"

go deeper

for a junior

Know that rust-toolchain.toml pins the toolchain for a repository and that cargo +nightly runs one command on another toolchain.

for a middle

Explain the keys (channel or path, components, targets, profile), the precedence order, and the closer-file rule when walking up directories.

for a senior

Show the operational side: pin an exact release for reproducible CI, bump it by pull request, commit Cargo.lock with it, and keep nightly jobs off the shared file.

for a principal

Separate what the repository builds with from what the crate promises users, and decide how often the team moves the pin.

## Why pin a toolchain at all A Rust project built on whatever stable compiler each machine happens to have can change behaviour on the day a new release ships: a new lint fires, a warning turns into a CI failure, or two developers see different diagnostics. Projects that need **one known compiler** everywhere record it in the repository, in a file named `rust-toolchain.toml` at the project root. rustup, the toolchain installer, reads it whenever one of its proxies (`cargo`, `rustc`, `rustfmt` and the rest) runs inside that directory tree, so everyone who clones the repository uses the same toolchain without being told. ## The file's keys The file is TOML and its **`[toolchain]` table is mandatory**, with at least one key set: | Key | Value | Effect | |---|---|---| | `channel` | `stable`, `beta`, `nightly`, a version such as `1.99` or `1.99.1`, optionally a date, as in `nightly-2026-10-25` | which toolchain to run | | `path` | absolute path to a custom toolchain | runs that local toolchain; the other keys have no effect | | `components` | list, e.g. `["clippy", "rustfmt"]` | extra components, **added to** the profile's set | | `targets` | list, e.g. `["wasm32-unknown-unknown"]` | extra compilation targets; the host is always included | | `profile` | `minimal`, `default` or `complete` | which group of components to install | Two rules trip people up: - `channel` and `path` are **mutually exclusive**. - If a directory holds both the legacy `rust-toolchain` file and `rust-toolchain.toml`, rustup uses the legacy `rust-toolchain` file for backwards compatibility. Two more details matter when the file names components. Leaving out `profile` does not guarantee the `default` profile, because a developer may have set a different one with `rustup set profile`. And components differ between toolchains: a nightly build may be published without a non-default component such as clippy, so a file that pins a dated nightly and lists clippy should name a date on which that component exists. ## Which setting wins rustup chooses a toolchain from these sources, taking the first one that is set: 1. the **toolchain shorthand** on the command line, as in `cargo +beta test`; 2. the **`RUSTUP_TOOLCHAIN`** environment variable; 3. a **directory override** set with `rustup override set`, or the **`rust-toolchain.toml`** file; 4. the **default toolchain**, set with `rustup default`. The third step has a twist: directory overrides and toolchain files are both found by walking up toward the filesystem root, and whichever is **closer** to the current directory wins. `rustup show` reports the active toolchain and why it was chosen. In practice the shorthand is how a developer runs one command on another toolchain, for example `cargo +nightly miri test`, without editing the file everyone shares. ## Pinning a stable release A team that wants reproducible CI pins an exact release such as `channel = "1.99.1"` and moves it forward with an ordinary pull request after reading the release notes. Things to know when doing so: - **A pinned version is its own toolchain.** `1.99.1-aarch64-apple-darwin` is installed separately from `stable-aarch64-apple-darwin`, even when both are the same release. With rustup's auto-install turned off, a machine that has only `stable` reports "toolchain '1.99.1-aarch64-apple-darwin' is not installed" and suggests `rustup toolchain install`, which installs the toolchain the file names. - **Commit the lockfile too.** The rustup documentation suggests tracking `Cargo.lock` when the toolchain is pinned to a specific release, so dependency versions cannot drift past what that compiler supports. - **Floating `stable` is a choice, not a default to fear.** Leaving `channel = "stable"` keeps the components and targets list while accepting each new release as soon as a machine updates its stable toolchain. ## The pin is not the minimum supported version `rust-toolchain.toml` says which compiler **this repository builds with**. It says nothing to the crate's users, and it does not travel when the crate is published as a dependency. A library's **minimum supported Rust version** is a separate promise, recorded in Cargo's `rust-version` key and verified by a CI job on that older toolchain. A common setup pins the development toolchain to a recent stable in the file, runs one CI job on the minimum supported version, and keeps any nightly experiment in a job that names its toolchain with `+nightly-...` instead of changing the shared file.

  • In a Rust monorepo, a sub-project has its own `rust-toolchain.toml` and the root has another; which one applies inside the sub-project?
    The closer one. rustup walks up from the current directory, and between toolchain files and directory overrides it prefers whichever is nearest. Inside the sub-project its own file wins, while commands run elsewhere in the repository use the root file. `rustup show` confirms which file was chosen.
  • Why might a Rust team pin `channel = "1.99.1"` instead of leaving `channel = "stable"`?
    A floating `stable` changes on release day, so a new lint or a compatibility change can turn CI red with no code change. An exact version makes every machine and CI run use the same compiler, and the team upgrades with a pull request it can test and review. The cost is remembering to bump it.

  • rust-toolchain.toml sets the minimum Rust version for the crate's users.
  • Listing components replaces the profile's components instead of adding to them.
  • A pinned 1.99.1 reuses the installed stable toolchain because the versions match.
  • Nothing can override rust-toolchain.toml short of editing or deleting it.
  • Cargo reads rust-toolchain.toml; rustup is not involved.