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 / Node | Rust | Purpose |
|---|---|---|
nvm / volta | rustup | Manage language/toolchain versions |
node | cargo run | Execute a program |
tsc | cargo build | Compile source to output |
npm install | cargo add <crate> | Add a dependency |
package.json | Cargo.toml | Project manifest and dependency list |
node_modules/ | ~/.cargo/registry/ + target/ | Cached crates and build artifacts |
npm run <script> | cargo run / custom tasks | Run project scripts |
npx tsc --init | cargo new <name> | Scaffold a new project |
eslint / prettier | cargo clippy / cargo fmt | Linter and formatter |
jest / vitest | cargo test | Run tests |
Installing Rust
Section titled “Installing Rust”The official way to install Rust is via rustup, the Rust toolchain installer. Run this in your terminal:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shFollow the prompts (the default installation is fine). Then restart your shell or run:
source $HOME/.cargo/envVerify the installation:
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.
Your first project — cargo new
Section titled “Your first project — cargo new”The cargo new command scaffolds a project, just like npm init but with more convention built in:
# TypeScript workflowmkdir my-project && cd my-projectnpm init -y # creates package.jsonnpm install typescript --save-devnpx tsc --init # creates tsconfig.json# ... write src/index.ts ...npx tsc # compilenode dist/index.js # run# Rust workflowcargo new my-project # creates the projectcd my-project# ... edit src/main.rs ...cargo run # compile + run in one stepcargo build # compile only (output in target/)cargo build --release # optimised production buildcargo add — adding dependencies
Section titled “cargo add — adding dependencies”Adding a dependency in Rust is a single command:
cargo add serde # adds serde to Cargo.tomlcargo add serde --features derive # with a feature flagcargo add tokio --features full # tokio async runtimeThis is the equivalent of npm install serde. The Cargo.toml file is updated automatically, just like package.json.
The key cargo commands
Section titled “The key cargo commands”cargo new <name> # scaffold a binary projectcargo new --lib <name> # scaffold a librarycargo build # compile (debug mode, fast)cargo build --release # compile (release mode, optimised)cargo run # compile + runcargo test # run testscargo add <crate> # add a dependencycargo fmt # format all source filescargo clippy # lint (like eslint for Rust)cargo doc --open # build and open local docscargo 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.
Updating Rust
Section titled “Updating Rust”To update your installed toolchain:
rustup update # update stable (and any other installed toolchains)rustup show # list installed toolchainsrustup default stable # set the default toolchainThis is the equivalent of nvm install --lts && nvm use --lts.