Skip to content

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:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Follow the default prompts (option 1). When done, restart your terminal or run:

source "$HOME/.cargo/env"

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:

rustc --version
cargo --version

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:

cd ~/code        # or wherever
cargo new hello
cd hello

That creates:

hello/
├── Cargo.toml      # the project manifest
└── src/
    └── main.rs     # the source

Open src/main.rs:

fn main() {
    println!("Hello, world!");
}

That's the whole file. Cargo created it for you.

Step 4: Build and run

cargo 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() {
    println!("Hello, world!");
}
  • fn main() - defines a function called main. Like Java/C, main is special - it's where the program starts.
  • println!("Hello, world!"); - calls the println macro 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

  1. Change the message; cargo run again. (Cargo recompiles, then runs.)

  2. Add a second line:

    fn main() {
        println!("Hello, world!");
        println!("this is my second line");
    }
    

  3. Try printing a number - note the format string:

    println!("{}", 42);
    
    The {} is a placeholder, similar to f-strings. We'll see more in page 02.

  4. Break it on purpose. Remove a semicolon. Run cargo check - read the error.

  5. Restore. Now mistype println as printl. Run cargo 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:

[package]
name = "hello"
version = "0.1.0"
edition = "2021"

[dependencies]
  • name / version - your project's identity.
  • edition - Rust's "edition" system. Lets the language evolve syntax without breaking old code. Current is 2021; 2024 exists; 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:

cargo check

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.

Next: First real program →

Comments