Rust - Installation & Tooling

Overview

Estimated time: 30–45 minutes

Learn how to install Rust using rustup, set up your development environment, and verify everything works correctly. We'll also explore the essential tools that come with Rust.

Learning Objectives

Prerequisites

Installing Rust with rustup

What is rustup?

rustup is the official installer and version management tool for Rust. It manages:

Installation on Windows

  1. Visit rustup.rs
  2. Download and run rustup-init.exe
  3. Follow the installer prompts (choose default options for beginners)
  4. Restart your command prompt or PowerShell

Installation on macOS

Open Terminal and run:

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

Then restart your terminal or run:

source ~/.cargo/env

Installation on Linux

Open your terminal and run:

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

Then restart your terminal or run:

source ~/.cargo/env

Verifying Your Installation

Check Rust Version

rustc --version

Expected Output:

rustc 1.70.0 (90c541806 2023-05-31)

Check Cargo Version

cargo --version

Expected Output:

cargo 1.70.0 (ec8a8a0ca 2023-04-25)

Test with Hello World

Create a simple program to test everything works:

mkdir rust-test
cd rust-test
echo 'fn main() { println!("Hello, Rust!"); }' > main.rs
rustc main.rs
./main       # On Windows: main.exe

Expected Output:

Hello, Rust!

Essential Rust Tools

rustc - The Rust Compiler

The core compiler that transforms Rust source code into executable programs.

Basic Usage

# Compile a single file
rustc main.rs

# Compile with optimization
rustc -O main.rs

# Compile for a specific target
rustc --target x86_64-pc-windows-gnu main.rs

cargo - Package Manager and Build Tool

Cargo handles project creation, dependency management, building, testing, and publishing.

Key Commands

# Create a new project
cargo new my_project
cd my_project

# Build your project
cargo build

# Build and run
cargo run

# Run tests
cargo test

# Check if code compiles (faster than build)
cargo check

# Build with optimizations
cargo build --release

rustfmt - Code Formatter

Automatically formats your Rust code according to style guidelines.

Usage

# Format a single file
rustfmt src/main.rs

# Format entire project
cargo fmt

# Check formatting without making changes
cargo fmt -- --check

Example: Before and After

Before formatting:

fn main(){let x=5;let y=10;println!("Sum: {}",x+y);}

After running rustfmt:

fn main() {
    let x = 5;
    let y = 10;
    println!("Sum: {}", x + y);
}

clippy - Linter and Code Analysis

Catches common mistakes and suggests improvements to your Rust code.

Installation and Usage

# Install clippy (if not already installed)
rustup component add clippy

# Run clippy on your project
cargo clippy

# Run clippy with extra strictness
cargo clippy -- -W clippy::all

Example: Clippy Suggestions

Code with issues:

fn main() {
    let x = 5;
    if x == 5 {
        println!("x is 5");
    }
    
    let vec = vec![1, 2, 3];
    for i in 0..vec.len() {
        println!("{}", vec[i]);
    }
}

Clippy output:

warning: for loop over a range of indices is not idiomatic
  --> src/main.rs:7:5
   |
7  |     for i in 0..vec.len() {
   |     ^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider using an iterator: `for item in &vec`

Setting Up Your Development Environment

Visual Studio Code (Recommended)

  1. Install VS Code
  2. Install the "rust-analyzer" extension
  3. Install the "CodeLLDB" extension for debugging

Useful VS Code Settings

Add to your settings.json:

{
    "rust-analyzer.checkOnSave.command": "clippy",
    "rust-analyzer.cargo.autoreload": true,
    "editor.formatOnSave": true,
    "[rust]": {
        "editor.defaultFormatter": "rust-lang.rust-analyzer"
    }
}

IntelliJ IDEA / CLion

  1. Install IntelliJ IDEA or CLion
  2. Install the "Rust" plugin
  3. Configure toolchain in Settings → Languages & Frameworks → Rust

Vim/Neovim

For Vim users, consider these plugins:

Managing Rust Versions

rustup Commands

# Show installed toolchains
rustup show

# Update to latest stable
rustup update

# Install nightly toolchain
rustup install nightly

# Set default toolchain
rustup default stable

# Use specific toolchain for a project
rustup override set nightly

# Add target for cross-compilation
rustup target add wasm32-unknown-unknown

Rust Release Channels

Creating Your First Cargo Project

Project Structure

cargo new hello_cargo
cd hello_cargo

This creates:

hello_cargo/
├── Cargo.toml
└── src/
    └── main.rs

Cargo.toml Configuration

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

[dependencies]
# Dependencies will go here

Default main.rs

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

Building and Running

# Build the project
cargo build

# Build and run in one command
cargo run

# Build for release (optimized)
cargo build --release

Expected Output:

   Compiling hello_cargo v0.1.0 (/path/to/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/hello_cargo`
Hello, world!

Understanding Cargo Build Outputs

Debug vs Release Builds

Build Artifacts

target/
├── debug/
│   ├── hello_cargo       # Debug executable
│   └── incremental/      # Incremental compilation cache
└── release/
    └── hello_cargo       # Release executable

Common Issues and Solutions

PATH Not Updated

Problem: Command not found after installation

Solution: Restart terminal or manually add to PATH:

# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.cargo/bin:$PATH"

Network Issues

Problem: Download fails behind corporate firewall

Solution: Configure proxy or download offline installer

Linker Errors on Windows

Problem: Missing Visual Studio Build Tools

Solution: Install Microsoft C++ Build Tools or Visual Studio

Next Steps

Now that Rust is installed and configured:

  1. Create a test project with cargo new
  2. Experiment with cargo run, cargo build, and cargo check
  3. Try formatting code with cargo fmt
  4. Run clippy with cargo clippy
  5. Explore your editor's Rust support

Common Pitfalls

Checks for Understanding

  1. What is rustup and why is it recommended over direct installation?
  2. What's the difference between cargo build and cargo run?
  3. How do you format Rust code automatically?
  4. What tool helps catch common mistakes in Rust code?
  5. What are the three main Rust release channels?

Answers

  1. rustup manages Rust versions, toolchains, and components, making updates and multi-version support easy
  2. cargo build compiles the project, cargo run compiles and executes it
  3. Use rustfmt directly or cargo fmt for the entire project
  4. clippy - the Rust linter that suggests improvements
  5. Stable, Beta, and Nightly

← PreviousNext →