Skip to content

Setup — Installing Rust and the Toolchain

Playground note: This is a setup and toolchain lesson — the commands run in your terminal, not in the browser. No runnable Playground snippet applies here.

From npm to cargo — the toolchain comparison

Section titled “From npm to cargo — the toolchain comparison”

As a TypeScript developer you already have a mental model for a language toolchain: nvm or volta manages Node versions, npm/pnpm/yarn manages packages, tsc compiles TypeScript, and node runs the output. Rust has a clean, single-tool answer for all of these: rustup and cargo.

TypeScript / NodeRustPurpose
nvm / voltarustupManage language/toolchain versions
nodecargo runExecute a program
tsccargo buildCompile source to output
npm installcargo add <crate>Add a dependency
package.jsonCargo.tomlProject manifest and dependency list
node_modules/~/.cargo/registry/ + target/Cached crates and build artifacts
npm run <script>cargo run / custom tasksRun project scripts
npx tsc --initcargo new <name>Scaffold a new project
eslint / prettiercargo clippy / cargo fmtLinter and formatter
jest / vitestcargo testRun tests

The official way to install Rust is via rustup, the Rust toolchain installer. Run this in your terminal:

Terminal window
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Follow the prompts (the default installation is fine). Then restart your shell or run:

Terminal window
source $HOME/.cargo/env

Verify the installation:

Terminal window
rustc --version # rustc 1.xx.x (...)
cargo --version # cargo 1.xx.x (...)

rustup installs the stable toolchain by default. You can add nightly later if you need experimental features — but stable is all you need for this course.

The cargo new command scaffolds a project, just like npm init but with more convention built in:

TypeScript
# TypeScript workflow
mkdir my-project && cd my-project
npm init -y # creates package.json
npm install typescript --save-dev
npx tsc --init # creates tsconfig.json
# ... write src/index.ts ...
npx tsc # compile
node dist/index.js # run
Rust
# Rust workflow
cargo new my-project # creates the project
cd my-project
# ... edit src/main.rs ...
cargo run # compile + run in one step
cargo build # compile only (output in target/)
cargo build --release # optimised production build

Adding a dependency in Rust is a single command:

Terminal window
cargo add serde # adds serde to Cargo.toml
cargo add serde --features derive # with a feature flag
cargo add tokio --features full # tokio async runtime

This is the equivalent of npm install serde. The Cargo.toml file is updated automatically, just like package.json.

Terminal window
cargo new <name> # scaffold a binary project
cargo new --lib <name> # scaffold a library
cargo build # compile (debug mode, fast)
cargo build --release # compile (release mode, optimised)
cargo run # compile + run
cargo test # run tests
cargo add <crate> # add a dependency
cargo fmt # format all source files
cargo clippy # lint (like eslint for Rust)
cargo doc --open # build and open local docs
cargo check # type-check without producing a binary (fast)

cargo check is especially useful: it runs the borrow checker and type checker without actually compiling to machine code, making it much faster than cargo build. This is your go-to command when iterating on code — the equivalent of running tsc --noEmit.

To update your installed toolchain:

Terminal window
rustup update # update stable (and any other installed toolchains)
rustup show # list installed toolchains
rustup default stable # set the default toolchain

This is the equivalent of nvm install --lts && nvm use --lts.

What is the Rust equivalent of nvm or volta for managing toolchain versions?
Which command compiles and immediately runs your Rust project?
What is the fastest way to type-check a Rust project without producing a binary?
How do you add a dependency called `serde` to a Rust project?