Sub topic 1
What rustup, rustc, and Cargo each do
Installing Rust gives you several programs, and three of them do most of the work: rustup, rustc, and cargo. They are separate tools with separate jobs. Knowing which one is responsible for what makes error messages easier to read and tells you where to look when something breaks. This chapter explains each tool, how they call one another, and where their files live. Installation itself comes in the next chapter.
Prerequisites
This chapter assumes you have:
- Comfort using a terminal and running shell commands.
- A basic idea of what a compiler does (turns source code into an executable) and what a package manager does (fetches and versions libraries).
- A computer running Linux, macOS, or Windows with internet access. You can read this chapter without Rust installed, but the commands are easier to follow if you try them once you have it.
The three tools at a glance
| Tool | Role | Typical question it answers |
|---|---|---|
| `rustup` | Toolchain installer and version manager | Which version of Rust is installed, and which one is active? |
| `rustc` | The Rust compiler | How do I turn this crate's source into a binary or library? |
| `cargo` | Build system and package manager | How do I build, test, and run a project and its dependencies? |
A toolchain is a complete set of Rust tools at one version. It contains rustc, cargo, the standard library, documentation, and optional components such as Clippy. rustup installs and switches between toolchains. Each toolchain's rustc and cargo do the actual work.
Read the diagram from top to bottom. You usually type cargo, but the program that answers is a small rustup proxy. The proxy picks a toolchain, and cargo from that toolchain calls rustc from the same toolchain.
rustup: the toolchain manager
rustup is the official installer and updater for Rust. It does not compile anything. Its job is to keep the right compilers and tools on disk and to decide which ones run when you type a command.
What rustup manages
- Toolchains. Each one is a full set of tools at a given version, such as
stable,beta,nightly, or a pinned release like1.75.0. - Channels. A channel is a release stream.
stableis released every six weeks.betais the next stable release in testing.nightlyis built every night and includes unfinished features that can change or disappear. - Components. These are optional parts of a toolchain, for example
clippy(a linter),rustfmt(a formatter), andrust-src(the standard library source). - Targets. A target is a platform you compile for. By default you build for your own machine.
rustup target addadds the standard library for another platform, such as WebAssembly, so you can cross-compile.
Proxies
When you install with rustup, the commands rustc, cargo, rustfmt, and others are not the real programs. They are small proxies placed in ~/.cargo/bin (or %USERPROFILE%\.cargo\bin on Windows). Each proxy asks rustup which toolchain applies, then runs that toolchain's real program.
You can see the real location with:
rustup which rustc
rustup showrustup which rustc prints the path of the actual compiler binary for the active toolchain, usually under ~/.rustup/toolchains/. rustup show lists installed toolchains and says which one is active and why.
Proxies are what make per-command switching possible. A leading +toolchain argument overrides the default for one call:
cargo +nightly --version
rustc +1.75.0 --versionThese commands only work if the named toolchain is installed.
How rustup chooses a toolchain
rustup checks these sources in order and uses the first one that applies:
- A
+toolchainargument on the command line. - The
RUSTUP_TOOLCHAINenvironment variable. - A directory override set with
rustup override set. - A
rust-toolchain.tomlfile in the current directory or a parent directory. - The default toolchain, set with
rustup default.
A rust-toolchain.toml file lets a project pin its toolchain so that everyone who builds it uses the same compiler:
[toolchain]
channel = "1.75.0"
components = ["clippy", "rustfmt"]When rustup sees this file, it uses that version for any command run inside the project. If the toolchain is missing, recent versions of rustup install it on first use.
Where the files live
rustup keeps its data in two places. You can move either one with an environment variable.
| Directory | Default | Holds | Override |
|---|---|---|---|
| Rustup home | `~/.rustup` | Installed toolchains and settings | `RUSTUP_HOME` |
| Cargo home | `~/.cargo` | Proxies in `bin/`, the downloaded crate cache, and Cargo configuration | `CARGO_HOME` |
rustc: the compiler
rustc is the Rust compiler. It takes the source files of one crate and produces either an executable or a library. A crate is the unit of compilation in Rust. A crate has a root file (such as main.rs or lib.rs), and the compiler follows the module declarations from that file to find the rest of the source.
You can call rustc directly without Cargo. Save this as hello.rs:
fn main() {
println!("Hello from rustc!");
}Then compile and run it:
rustc hello.rs
./helloOn Windows the output is hello.exe, so run .\hello.exe instead. The compiler produced the program in a single step, with no project folder and no configuration file.
What rustc does and does not do
rustc checks syntax, types, and the borrow rules, then generates machine code through LLVM and calls the system linker to join the pieces into a final binary. A linker is a separate program that combines compiled object files and libraries into one executable. Rust does not ship its own linker on most platforms, which is why a missing C toolchain causes installation problems that a later chapter covers.
rustc does not do any of the following:
- Download dependencies.
- Decide which files in a project need rebuilding.
- Run tests as a project workflow.
- Know about
Cargo.toml.
Every flag and every dependency path must be given on the command line. That is manageable for one file and impractical for a project with dozens of dependencies. This is the gap Cargo fills.
Editions
An edition is an opt-in set of language changes that the compiler applies to a crate. Editions such as 2018, 2021, and 2024 let Rust make changes that would otherwise break old code. Crates built with different editions can be linked together. With rustc you select the edition with a flag, for example rustc --edition 2021 hello.rs. With Cargo you set it in Cargo.toml.
Cargo: the build system and package manager
cargo is the command you will use most. It does two jobs:
- Build system. It decides what to compile, in what order, and with what options. It runs
rustcfor you, once per crate, and skips work when nothing has changed. - Package manager. It reads your dependencies, downloads them from [crates.io](https://crates.io) (the public Rust package registry), builds them, and records the exact versions it used.
A project managed by Cargo is called a package. Its settings live in a file named Cargo.toml, called the manifest:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]Cargo also writes a file named Cargo.lock, which pins the exact version of every dependency so that builds are repeatable. Compiled output goes into a target/ directory. Later chapters cover each of these files in detail.
Cargo calls rustc
Cargo does not compile code itself. You can confirm this by building a project with the verbose flag:
cargo build --verboseThe output includes lines starting with Running followed by a full rustc command. They list the crate name, the edition, the output directory, and the paths of any dependencies. Cargo is assembling the long command line that you would otherwise write by hand.
The common Cargo commands
| Command | What it does |
|---|---|
| `cargo new` | Creates a new package |
| `cargo check` | Type-checks the code without producing a binary |
| `cargo build` | Compiles the package |
| `cargo run` | Builds if needed, then runs the binary |
| `cargo test` | Builds in test mode and runs tests |
| `cargo add` | Adds a dependency to `Cargo.toml` |
Other tools plug into the same interface. cargo fmt runs rustfmt, and cargo clippy runs Clippy. Both are components that rustup installs.
How a single command passes through all three
Take cargo run in a project that contains a rust-toolchain.toml file.
- The shell finds
cargoin~/.cargo/bin. That file is arustupproxy. - The proxy reads
rust-toolchain.toml, picks the pinned toolchain, and launches that toolchain's realcargo. cargoreadsCargo.tomlandCargo.lockand works out which crates need building.- For each crate,
cargoruns therustcfrom the same toolchain. rustccompiles and calls the linker. The result lands intarget/debug/.cargoruns the binary.
Common mistakes
- Mixing compilers. If your operating system's package manager also installed
rustc, a different copy may appear earlier on yourPATHthan~/.cargo/bin. Usewhich rustc(orwhere rustcon Windows) to see which one runs. Updates throughrustupwill not change a copy installed by another tool. - Expecting
rustup updateto update your project. It updates toolchains only. Your dependency versions are handled by Cargo andCargo.lock. - Calling
rustcon a Cargo project. This skips the dependencies and settings fromCargo.toml. Usecargo buildand let Cargo run the compiler. - Using
+nightlywithout installing it. The command fails until you runrustup toolchain install nightly. - Reading a version number as the whole story.
cargo --versionandrustc --versionreport the toolchain they belong to. If they ever disagree, you are probably running tools from different installations.
Checking your understanding
Exercise
You run cargo build in a project with a rust-toolchain.toml that pins version 1.75.0, but your default toolchain is stable. Which program reads the pin file, and which program compiles the code?
Show answer
The rustup proxy for cargo reads rust-toolchain.toml and launches the cargo from the 1.75.0 toolchain. That cargo then runs the 1.75.0 rustc, which compiles the code and calls the linker.
Exercise
A teammate says, "I ran rustup update and now my dependencies are newer." Is that accurate? Name the tool that is actually responsible for dependency versions and the file where they are recorded.
Show answer
It is not accurate. rustup update only updates installed toolchains. Cargo is responsible for dependency versions, and it records the exact versions in Cargo.lock. Dependencies change when you run a command such as cargo update or edit Cargo.toml.
Exercise
How can you show that Cargo uses rustc instead of compiling code itself?
Show answer
Run cargo build --verbose in a project. The output contains Running lines with full rustc commands, including the crate name, edition, and dependency paths.