Sub topic 1
What rustup, rustc, and Cargo each do
Prerequisites
This chapter assumes you can run commands in a terminal and know what a compiler and a package manager do. You don't need Rust installed yet. The next chapter covers installation.
Three tools, three jobs
A standard Rust setup has three tools that you will use together. They are easy to mix up because you often type one command and another tool does the work.
- rustup is the toolchain manager. It installs Rust, keeps it up to date, and lets you switch between versions.
- rustc is the Rust compiler. It turns Rust source code into an executable program or a library.
- Cargo is the build tool and package manager. It creates projects, calls
rustcfor you, downloads dependencies, and runs tests.
A toolchain is one complete set of Rust tools for a given version. It includes rustc, Cargo, the standard library, and optional components such as Clippy and rustfmt.
rustup: installs and selects toolchains
You install rustup first. It then installs everything else. Because it manages versions, you can keep a stable toolchain for daily work and a nightly one for experiments. You can also pin a project to an older release.
The commands rustc and cargo that you type are small proxy programs that rustup places in ~/.cargo/bin. Each proxy looks up which toolchain is active and forwards your command to the matching real binary. This is why the same cargo command can use different Rust versions in different directories.
rustc: the compiler
rustc reads Rust source files and produces machine code. You can call it directly on a single file:
rustc main.rs
./mainThis works for tiny programs. Real projects have many files, external crates, and different build settings. Passing all of that to rustc by hand is tedious and error-prone, so you rarely do it.
Cargo: the tool you use every day
Cargo reads a file named Cargo.toml that describes your project. From that file it works out what to compile, which dependencies to fetch, and which flags to pass to rustc.
Common Cargo jobs include:
- Creating a project with
cargo new. - Building and running it with
cargo buildandcargo run. - Adding dependencies from crates.io, the public package registry.
- Running tests with
cargo test.
Cargo does not compile code itself. It runs rustc for each crate and passes the arguments for you.
How the pieces connect
The diagram below shows the chain of calls when you type cargo run.
You can see the same split by asking each tool for its version:
rustup --version
cargo --version
rustc --versionEach command prints its own version string, because each tool is a separate program.
Common points of confusion
- "Cargo compiles my code." Cargo orchestrates the build.
rustcdoes the compiling. - "Updating Rust means updating Cargo." You update through
rustup update, which replaces the whole toolchain, including bothrustcand Cargo. - "I installed Rust with my system package manager, so rustup is optional." Distribution packages often lag behind and lack rustup's channel switching. This tutorial uses
rustupthroughout.
Check your understanding
Exercise
You type cargo build and the project compiles. Which tool picked the Rust version used, which tool decided what to compile, and which tool produced the machine code?
Show answer
rustup picked the version, because its cargo proxy forwards to the active toolchain. Cargo decided what to compile, using Cargo.toml. rustc produced the machine code.
Exercise
Why would you rarely run rustc directly on a real project?
Show answer
Real projects have multiple files, dependencies, and build settings. Cargo reads Cargo.toml and passes the right arguments to rustc, so you don't have to write them by hand.