01 - Setup¶
What this session is¶
About 30 minutes. You'll install Rust, write your first program, and meet Cargo - Rust's build tool, package manager, test runner, and documentation generator, all in one binary. Rust's tooling is one of its best features; you'll appreciate it immediately.
Step 1: Install Rust via rustup¶
rustup is the official installer. It manages Rust toolchain versions and components.
macOS / Linux:
Follow the default prompts (option 1). When done, restart your terminal or run:
Windows: download rustup-init.exe from rustup.rs, run it, follow the prompts. You may need Visual Studio Build Tools for the C linker - the installer guides you.
Verify:
You should see version numbers around 1.80 or later.
Step 2: Install rust-analyzer in VS Code¶
Open VS Code → Extensions → search "rust-analyzer" → Install. This gives you inline error highlighting, autocompletion, go-to-definition, and "explain this error" - invaluable when learning.
Step 3: Your first program with Cargo¶
Cargo creates a new project skeleton for you:
That creates:
Open src/main.rs:
That's the whole file. Cargo created it for you.
Step 4: Build and run¶
You'll see Cargo compile your project, then:
Compiling hello v0.1.0 (...)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.55s
Running `target/debug/hello`
Hello, world!
That's your first Rust program.
cargo run is the daily driver. It compiles and runs in one step. Other useful commands:
| Command | What it does |
|---|---|
cargo new <name> |
Create a new project. |
cargo build |
Compile (don't run). Output in target/debug/. |
cargo build --release |
Optimized build. Output in target/release/. Slower to compile, faster at runtime. |
cargo check |
Type-check without compiling fully. Much faster than cargo build. Use this during development. |
cargo test |
Run tests. |
cargo doc --open |
Generate and open documentation. |
cargo clippy |
Run the linter. |
cargo fmt |
Auto-format your code. |
cargo update |
Update dependencies (and Cargo.lock). |
What just happened - line by line¶
fn main()- defines a function calledmain. Like Java/C,mainis special - it's where the program starts.println!("Hello, world!");- calls theprintlnmacro with the string"Hello, world!".- The
!makes this a macro, not a function. Macros are code that expands into other code at compile time.println!exists as a macro (instead of a function) because it takes a variable number of arguments with type checking. You'll meet a few macros (vec!,format!,assert_eq!); the!is the visual signal. - The
;ends a statement. - Curly braces
{ }delimit the function body.
Simpler than Java; about as much as Go. Rust gives you a hello world in 3 lines and a project skeleton.
Try changing things¶
-
Change the message;
cargo runagain. (Cargo recompiles, then runs.) -
Add a second line:
-
Try printing a number - note the format string:
The{}is a placeholder, similar to f-strings. We'll see more in page 02. -
Break it on purpose. Remove a semicolon. Run
cargo check- read the error. -
Restore. Now mistype
printlnasprintl. Runcargo check. The compiler error tells you exactly what's wrong, often with a suggested fix.
Rust's compiler errors are widely considered the best in the industry. Read them. They teach you.
The Cargo.toml file¶
Open it. You should see:
name/version- your project's identity.edition- Rust's "edition" system. Lets the language evolve syntax without breaking old code. Current is2021;2024exists; the differences are minor for learners.[dependencies]- empty for now. We'll add things in page 11.
Try cargo check¶
The fastest feedback loop in Rust:
It type-checks your code without generating a final binary. Twice as fast as cargo build. Use it during development; reserve cargo run / cargo build for when you actually want to execute.
What you might wonder¶
"Why both cargo run and cargo build?"
cargo run = build + run. cargo build just produces a binary in target/debug/<projectname>. Useful when you want to ship the binary or run it through other tools.
"Why dev vs release builds?"
Dev builds: fast to compile, slow to run, with debug info. Release builds: slow to compile, much faster to run, optimized. Use dev for development; build release for shipping.
"Why is the binary in target/debug/?"
Cargo organizes output by profile. target/debug/ for dev builds, target/release/ for release. Add target/ to .gitignore (Cargo's cargo new already did).
"Should I use cargo check or cargo build?"
Use cargo check while iterating; cargo build or cargo run when you want to actually execute the program. The speed difference matters as your project grows.
Done¶
You have: - Rust installed via rustup. - Cargo (which came with rustup). - A working "hello world" project. - rust-analyzer in your editor for inline errors.
Next page: the real learning starts.