Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Setup & Toolchain

Goal: you can verify a working Rust toolchain and explain what each core cargo command does — in under 30 minutes.

Install

Use rustup (the official toolchain manager). On Linux/macOS:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

Verify:

rustc --version   # compiler — we standardize on 1.96.x
cargo --version   # build tool + package manager
rustup update     # run occasionally; Rust ships every 6 weeks

Why rustup, not apt? Distro packages are always stale. Rust’s release cadence is 6 weeks; rustup also manages multiple toolchains (stable / nightly / specific MSRV) and cross-compile targets. Everything CodeCora runs on ARM (aarch64) — rustup handles that transparently.

Editor

Any editor with rust-analyzer works. Minimum setup:

  • rust-analyzer (LSP): completions, inline types, go-to-definition through macros
  • rustfmt on save (the Format check in our CI is cargo fmt --all -- --check)
  • clippy as the lint source (CI runs cargo clippy --all-targets -- -D warnings)

The cargo commands you will use daily

CommandWhat it doesWhen
cargo new <name>scaffold a binary cratestarting any project
cargo runbuild + executeinner loop
cargo build --releaseoptimized buildperf / shipping
cargo testrun all testsbefore every commit
cargo checktype-check without codegenfastest feedback
cargo clippy -- -D warningslint; warnings are errorsbefore every commit
cargo fmtauto-format (rustfmt)before every commit
cargo add <crate>add a dependencywhen a crate earns its place

The house loop (burn this in — it is also our CI gate):

cargo fmt && cargo clippy --all-targets -- -D warnings && cargo test

Crates, packages, modules — 60 seconds

  • Package = a Cargo.toml (what cargo new creates). Builds one or more crates.
  • Crate = one compilation unit: a binary (src/main.rs) or a library (src/lib.rs).
  • Module = a namespace inside a crate (mod parser;src/parser.rs).
  • Dependency = someone else’s crate, from crates.io, pinned in Cargo.toml + locked in Cargo.lock (commit the lockfile for binaries).

Lab: clone and run the gate lab

git clone https://github.com/codecoradev/rust-lms
cd rust-lms/labs/gate-hello
cargo test          # expect: 2 passed
cargo run           # Hello from Rust 0.1.0 — toolchain works!
cargo run -- --add 40 2    # 40 + 2 = 42

Exercises

  1. Verify — run the house loop above in labs/gate-hello. All three must pass.
  2. Break it — change --add to print a - b instead. Re-run the loop.
  3. Explain — out loud, in one sentence each: what cargo check does that cargo build skips, and why that makes it faster.

Done when: the house loop is green after your change, and you ran the binary at least once.