Chapter 2: Getting Started

This chapter guides you through setting up your Rust development environment and writing your first program. By the end, you'll have a fully configured workspace and understand the basics of Cargo, Rust's build system and package manager.

2.1 Installing Rust

Rust is installed through rustup, the official Rust toolchain installer. It manages Rust versions and associated tools, making it easy to keep your installation up to date.

macOS and Linux

Open your terminal and run:

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

This command downloads and runs the rustup installer. Follow the on-screen prompts (usually pressing Enter for default options is fine).

1 Add Rust to your PATH

The installer will add the following to your shell configuration:

source "$HOME/.cargo/env"
2 Restart your terminal or run:
source ~/.cargo/env

Windows

1 Download the installer

Visit rustup.rs and download rustup-init.exe

2 Install Visual C++ Build Tools

Rust requires the Microsoft C++ Build Tools. The installer will prompt you to install them if needed.

3 Run the installer and follow the prompts

Verifying the Installation

After installation, verify everything works:

# Check Rust compiler version
rustc --version
# Example output: rustc 1.75.0 (82e1608df 2023-12-21)

# Check Cargo version
cargo --version
# Example output: cargo 1.75.0 (1d8b05cdd 2023-11-20)

# Check rustup version
rustup --version
# Example output: rustup 1.26.0 (5af9b9484 2023-04-05)
Installed Tools

rustup installs these essential components:

2.2 Updating and Managing Rust

Rust releases a new stable version every six weeks. Keep your installation current:

# Update Rust to the latest version
rustup update

# Show installed toolchains
rustup show

# Install a specific version
rustup install 1.70.0

# Switch default toolchain
rustup default stable

# Install nightly (for experimental features)
rustup install nightly
rustup run nightly cargo build
Stable vs Nightly

Use stable for production code. The nightly channel has experimental features that may change. Some tools (like rocket web framework) may require nightly.

2.3 IDE Setup

A good IDE makes Rust development much more productive. We recommend Visual Studio Code with the rust-analyzer extension.

Visual Studio Code Setup

1 Install VS Code

Download from code.visualstudio.com

2 Install rust-analyzer extension

Search for "rust-analyzer" in the Extensions panel (Ctrl+Shift+X) and install it.

3 Recommended additional extensions:

What rust-analyzer Provides

Feature Description
Code completion Smart suggestions as you type
Go to definition Jump to function/type definitions (F12)
Inline errors See compiler errors without building
Inlay hints Show types and parameter names inline
Rename refactoring Safely rename across project
Code actions Quick fixes and code generation

Other IDE Options

2.4 Your First Rust Program

Let's create and run your first Rust program using Cargo.

Creating a New Project

# Create a new project called hello_rust
cargo new hello_rust

# Navigate into the project
cd hello_rust

Cargo creates this project structure:

Understanding Cargo.toml

Open Cargo.toml - this is your project's manifest:

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

# See more keys and their definitions at
# https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Your external dependencies go here
Key Cargo.toml Sections

The Main File

Open src/main.rs:

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

Let's break this down:

Code Explanation
fn Keyword to define a function
main() The entry point - every Rust program starts here
{ } Function body enclosed in curly braces
println! A macro (note the !) that prints to console
"Hello, world!" A string literal
; Statements end with semicolons

Running the Program

# Build and run
cargo run

# Output:
   Compiling hello_rust v0.1.0 (/path/to/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.42s
     Running `target/debug/hello_rust`
Hello, world!
Congratulations!

You've just written and run your first Rust program!

2.5 Understanding Cargo Commands

Cargo is your constant companion in Rust development. Here are the essential commands:

Command What It Does
cargo new <name> Create a new project
cargo build Compile the project
cargo run Build and run
cargo check Check for errors without building (faster)
cargo test Run tests
cargo fmt Format code
cargo clippy Run linter for code improvements
cargo doc --open Generate and open documentation
cargo build --release Build optimized release version
cargo add <crate> Add a dependency

Debug vs Release Builds

# Debug build (fast compilation, slow execution, with debug symbols)
cargo build
# Binary: target/debug/hello_rust

# Release build (slow compilation, fast execution, optimized)
cargo build --release
# Binary: target/release/hello_rust
When to Use Release Mode

Use cargo run during development for fast iteration. Use cargo build --release for performance testing and deployment. The performance difference can be 10-100x!

2.6 Adding Dependencies

Rust's package ecosystem lives on crates.io. Let's add a dependency:

# Add the rand crate
cargo add rand

This updates your Cargo.toml:

[dependencies]
rand = "0.8.5"

Now use it in your code:

use rand::Rng;

fn main() {
    let secret_number = rand::thread_rng().gen_range(1..=100);
    println!("Secret number: {}", secret_number);
}

Understanding Version Numbers

Syntax Meaning
"0.8.5" Compatible with 0.8.5+ but less than 0.9.0
"^0.8.5" Same as above (caret is default)
"~0.8.5" Patch updates only (0.8.5 - 0.8.x)
"=0.8.5" Exactly this version
"*" Any version (not recommended)

2.7 Project Structure Best Practices

As your project grows, organize it properly:

Library vs Binary Crates

# Create a library project
cargo new my_library --lib

# Create a binary project (default)
cargo new my_app
Binary vs Library

2.8 Chapter Summary

Key Takeaways

2.9 Review Questions

Test Your Understanding

  1. What is rustup and why is it preferable to installing Rust from package managers?
  2. What's the difference between cargo build and cargo check? When would you use each?
  3. Explain the purpose of the Cargo.toml file. What sections does it contain?
  4. What does the ! in println! indicate?
  5. What's the difference between debug and release builds? When should you use each?
  6. How do you add an external dependency to a Rust project? What file is modified?

2.10 Looking Ahead

What's Next: Chapter 3 - Ownership

In the next chapter, you'll learn Rust's most important concept—ownership. This is what makes Rust unique and enables memory safety without garbage collection: