Rust Setup & Toolchain
rustup. It installs the compiler (rustc), the package manager and build tool (cargo), the standard library, and the formatting/linting tools together — so there is nothing to assemble by hand.
Installation
Install rustup for your platform, then let it set up the stable toolchain. On Windows use the rustup.rs installer or winget install Rustlang.Rustup; on macOS or Linux use the one-line script:
# macOS / Linux — follow the on-screen prompt
curl --proto '=https' --tls-v1.2 -sSf https://sh.rustup.rs | sh
# Windows (PowerShell / winget)
winget install Rustlang.Rustup
# verify everything landed
rustc --version # the compiler
cargo --version # the build tool + package manager
rustup update # keep the toolchain current
Because the whole toolchain lives under one rustup, you can install extra targets (e.g. rustup target add wasm32-unknown-unknown for WebAssembly) or pin a stable version per project.
First Program
Any program starts as a crate. Cargo scaffolds a binary crate with one command, already wired for building and testing. Let it generate the default project, then open src/main.rs:
cargo new hello # creates a binary crate named hello
cd hello
// src/main.rs — every binary crate starts at fn main()
fn main() {
// println! is a macro (note the '!'); it formats text to stdout
println!("Hello, world!");
}
Build and run in one step with cargo run. Cargo compiles the crate into target/debug/, then executes the resulting binary:
cargo run
# Compiling hello v0.1.0
# Finished `dev` profile [unoptimized + debuginfo]
# Running `target/debug/hello`
# Hello, world!
Project Structure
A minimal Cargo project looks like this. Cargo.toml is the manifest (name, version, dependencies); src/ holds source; Cargo.lock pins exact dependency versions and target/ holds build output (never edit either by hand):
hello/
├── Cargo.toml # project manifest + dependencies
├── Cargo.lock # resolved, pinned dependency versions
└── src/
└── main.rs # binary entry point (fn main)
The manifest is the single source of truth for a crate. A fresh one declares the package and the edition of the language it targets:
[package]
name = "hello"
version = "0.1.0"
edition = "2021" # language edition: 2015 / 2018 / 2021 / 2024
[dependencies]
# add crates here; e.g. serde = "1" — cargo fetches and builds them
Everyday Commands
You will cycle through a small set of commands. Learn them early so the toolchain fades into the background while you think about the language:
| Command | What it does |
|---|---|
cargo build | Compile (debug profile) without running. |
cargo run | Build, then execute the binary. |
cargo check | Type-check only — much faster than a full build, ideal while editing. |
cargo test | Compile and run unit + integration tests. |
cargo fmt | Format code to the official style. |
cargo clippy | Run the community linter for idiomatic, bug-free code. |
cargo doc --open | Generate and open documentation for your crate. |
cargo new, then deliberately break main.rs (drop a semicolon) and watch cargo check localize the error. Reading compiler diagnostics without panic is the first real Rust skill.