Installation and Cargo Basics
Install Rust with rustup, master Cargo commands, and write your first Hello World program
Installation and Cargo Basics
Before writing Rust code, you need the Rust toolchain. Rust makes this easy with rustup, the official installer and toolchain manager.
Installing Rust with rustup
Linux / macOS / Windows (WSL)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shThis installs:
- rustup β The toolchain manager
- rustc β The Rust compiler
- cargo β The build system and package manager
- rustdoc β The documentation generator
Windows (Standalone)
Download the installer from rustup.rs and run it. You'll also need the Visual Studio C++ Build Tools for linking.
Verify the Installation
rustc --version # rustc 1.85.0 (or later)
cargo --version # cargo 1.85.0
rustup --version # rustup 1.28.0Rust guarantees forward compatibility: code written for Rust 1.0 still compiles on the latest version. This is called "edition stability."
Managing Toolchains with rustup
# List installed toolchains
rustup toolchain list
# Switch to nightly (for experimental features)
rustup default nightly
# Install a specific version
rustup toolchain install 1.75.0
# Add the WebAssembly target
rustup target add wasm32-unknown-unknownRust Channels
| Channel | Use Case | Stability Guarantee |
|---|---|---|
| stable (default) | Production | Fully stable, every 6 weeks |
| beta | Testing upcoming features | Mostly stable |
| nightly | Experimental features | May break |
Your First Rust Project with Cargo
Cargo is Rust's build system and package manager. It handles:
- Creating new projects
- Building and compiling code
- Running tests
- Fetching dependencies
- Building documentation
cargo new
cargo new hello_rust
cd hello_rustThis creates:
hello_rust/
βββ Cargo.toml # Package manifest
βββ src/
β βββ main.rs # Entry point
βββ .git/ # Auto-initialized git repo
Cargo.toml β The Package Manifest
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"
[dependencies]| Field | Purpose |
|---|---|
name | Package name, used on crates.io |
version | Semantic versioning |
edition | Rust edition (2015, 2018, 2021, 2024) |
[dependencies] | External crate dependencies |
src/main.rs β Hello World
fn main() {
println!("Hello, world!");
}The main function is the program entry point. println! is a macro (indicated by !) that prints text with a newline.
cargo build
cargo buildCompiles the project. The binary goes to target/debug/hello_rust.
hello_rust/
βββ Cargo.toml
βββ Cargo.lock # Dependency lockfile
βββ src/
β βββ main.rs
βββ target/
βββ debug/
β βββ hello_rust # Binary
βββ ...
cargo run
cargo runBuilds (if needed) and runs the binary in one step:
Compiling hello_rust v0.1.0 (/path/to/hello_rust)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/hello_rust`
Hello, world!
cargo check
cargo checkChecks code for errors without producing a binary. Much faster than cargo build β use it frequently during development.
Use cargo check to quickly verify your code compiles. Only use cargo build or cargo run when you actually need the binary. This saves significant time on larger projects.
Additional Cargo Commands
| Command | Purpose |
|---|---|
cargo check | Type-check without building |
cargo build | Compile (debug mode) |
cargo build --release | Compile with optimizations |
cargo run | Build and run |
cargo test | Run tests |
cargo doc --open | Build and open documentation |
cargo fmt | Format code |
cargo clippy | Lint code |
cargo update | Update dependencies |
cargo clean | Remove target directory |
Release vs Debug Mode
| Mode | Command | Optimizations | Debug Info | Build Time |
|---|---|---|---|---|
| Debug | cargo build | None | Full | Fast |
| Release | cargo build --release | Level 3 | Minimal | Slow |
cargo build --release
# Binary is in target/release/hello_rustAlways benchmark and deploy the release build. Debug builds are significantly slower and larger.
Adding Dependencies
Edit Cargo.toml:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
rand = "0.8"Or use cargo add:
cargo add serde --features derive
cargo add randCargo fetches dependencies from crates.io, Rust's package registry.
Using Dependencies
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1..=100);
println!("Secret: {secret_number}");
}Workspaces (For Larger Projects)
For multi-crate projects:
# Cargo.toml (workspace root)
[workspace]
members = ["crate_a", "crate_b"]project/
βββ Cargo.toml # Workspace definition
βββ crate_a/
β βββ Cargo.toml
β βββ src/main.rs
βββ crate_b/
βββ Cargo.toml
βββ src/main.rs
Cargo Profiles
Customize compilation settings:
[profile.release]
opt-level = 3 # Max optimization
lto = true # Link-time optimization
codegen-units = 1 # Slower compile, faster code
strip = true # Remove debug symbols (smaller binary)Practice Questions
- What command installs Rust?
- What does
cargo newcreate? - What's the difference between
cargo checkandcargo build? - How do you compile with optimizations?
- What file declares a project's dependencies?
- What does the
[dependencies]section in Cargo.toml do? - How do you add the
serdecrate with derive features? - What's the difference between debug and release builds?
- How do you update all dependencies in a project?
- What command builds documentation and opens it in a browser?