2.0 What This Teaches
rustup- installing and managing Rust versionscargo- the build system and package managerrustfmt- automatic code formattingclippy- the Rust linterrust-analyzer- IDE language server
2.1 rustup - Managing Rust Versions
rustup is the Rust toolchain installer. It downloads and manages
versions of the Rust compiler (rustc), Cargo, and the standard library.
Install it from rustup.rs; on Windows, run the downloaded
rustup-init.exe.
rustup update // update to latest stable
rustup show // list installed toolchains
rustup target add wasm32-unknown-unknown // add a cross-compile target
rustup update
keeps you current without reinstalling anything.
2.2 cargo - Build and Package Manager
cargo is the tool you use for nearly everything: creating projects,
compiling, running tests, adding dependencies, and publishing crates. You already
used cargo new and cargo run in Tutorial 1.
cargo new MyProject --bin // create a binary crate
cargo new MyLib --lib // create a library crate
cargo build // compile (debug)
cargo build --release // compile (optimized)
cargo run // build and run
cargo test // build and run all tests
cargo check // type-check without producing a binary
cargo add serde // add a dependency to Cargo.toml
cargo doc --open // build and open HTML documentation
cargo check is faster than cargo build and is useful
during active editing to catch type errors quickly without waiting for code generation.
2.3 rustfmt - Code Formatter
rustfmt reformats Rust source files to match the official Rust style
guide. It is opinionated and non-negotiable about most rules, which eliminates
style debates on teams.
cargo fmt // format all files in the current crate
cargo fmt -- --check // report differences without writing files
rustfmt on save automatically when rust-analyzer is
configured. The style it enforces is the same style used throughout the Rust
standard library.
2.4 clippy - Linter
clippy is a collection of lints - analysis passes that detect common
mistakes and non-idiomatic patterns. It goes beyond what the compiler flags and
teaches Rust idioms as you work.
cargo clippy
Copy. Each diagnostic
names the lint and usually suggests an exact fix.
2.5 rust-analyzer - IDE Support
rust-analyzer is the language server for Rust. It connects to any
editor that supports the Language Server Protocol (LSP) - VS Code, Neovim, Emacs,
and others. It provides:
- Inline type annotations
- Completion and documentation on hover
- Go-to-definition and find-references
- Inline error and warning display
- Automatic
rustfmton save
rust-analyzer in the Extensions panel. No further configuration
is needed for standard Cargo projects.
2.6 Exercise
Exercise
In your
Hello project from Tutorial 1, run cargo clippy
and cargo fmt -- --check. Then deliberately introduce a style
inconsistency (extra spaces, wrong indentation) and run cargo fmt
to see it corrected automatically.
2.7 Common Mistakes
Skipping cargo check during editing
cargo build for every edit is slow. Use cargo check
in a terminal while editing - it catches type errors in a fraction of the time
without producing an executable.
Ignoring clippy warnings
#[allow(clippy::...)]
is sometimes correct, but should be a deliberate decision, not a reflex.
Running rustup update infrequently
2.8 Key Terms
| Term | Meaning |
|---|---|
| rustup | Toolchain installer and version manager for Rust |
| cargo | Build system, package manager, and test runner for Rust |
| rustfmt | Automatic code formatter enforcing the official Rust style |
| clippy | Linter that detects common mistakes and non-idiomatic patterns |
| rust-analyzer | Language server providing IDE features via the LSP protocol |
| LSP | Language Server Protocol - standard interface between editors and language tools |