Rust - Introduction

Overview

Estimated time: 30–45 minutes

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. This introduction orients you to Rust's unique approach to memory safety without garbage collection, its growing ecosystem, and what you'll learn in this comprehensive tutorial series.

Learning Objectives

Prerequisites

Why Rust?

Memory Safety Without Garbage Collection

Rust prevents common programming errors like null pointer dereferences, buffer overflows, and memory leaks at compile time, without the runtime overhead of a garbage collector.

Zero-Cost Abstractions

High-level features compile down to the same assembly you'd write by hand. You don't pay a runtime cost for abstractions.

Fearless Concurrency

Rust's ownership system enables safe concurrent programming by preventing data races at compile time.

Example: Hello, Rust!

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

Expected Output:

Hello, Rust!

Rust's Unique Features

Ownership System

Rust tracks ownership of memory through its type system, eliminating the need for manual memory management or garbage collection:

fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // s1 is moved to s2
    // println!("{}", s1); // This would cause a compile error!
    println!("{}", s2); // This works fine
}

Expected Output:

hello

Pattern Matching

Exhaustive pattern matching ensures you handle all possible cases:

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter,
}

fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter => 25,
    }
}

fn main() {
    let coin = Coin::Dime;
    println!("Value: {} cents", value_in_cents(coin));
}

Expected Output:

Value: 10 cents

Error Handling

Rust forces you to handle errors explicitly using Result and Option types:

fn divide(x: f64, y: f64) -> Result {
    if y == 0.0 {
        Err("Cannot divide by zero".to_string())
    } else {
        Ok(x / y)
    }
}

fn main() {
    match divide(10.0, 2.0) {
        Ok(result) => println!("Result: {}", result),
        Err(error) => println!("Error: {}", error),
    }
    
    match divide(10.0, 0.0) {
        Ok(result) => println!("Result: {}", result),
        Err(error) => println!("Error: {}", error),
    }
}

Expected Output:

Result: 5
Error: Cannot divide by zero

Common Use Cases for Rust

Systems Programming

Web Development

Command Line Tools

Blockchain & Cryptocurrency

The Rust Ecosystem

Cargo Package Manager

Cargo handles dependencies, builds, testing, and publishing. It's built into Rust from day one.

Crates.io

The official package registry with over 100,000 community crates (libraries).

Popular Crates

Rust Editions

Rust releases new "editions" every 3 years to introduce breaking changes in a controlled way:

Performance Characteristics

Memory Usage

Rust programs typically use less memory than garbage-collected languages because:

Runtime Performance

Rust often matches or exceeds C/C++ performance because:

Compile Times

Rust compilation is slower than some languages due to:

Learning Path Overview

Beginner Track (First 10 tutorials)

  1. Installation & tooling setup
  2. First program and basic syntax
  3. Variables, data types, and functions
  4. Control flow and pattern matching
  5. Ownership and borrowing concepts

Intermediate Track

Advanced Track

Architect Track

Common Pitfalls for Beginners

Fighting the Borrow Checker

New Rustaceans often struggle with ownership concepts. The borrow checker is your friend—it prevents bugs!

Over-using clone()

Cloning data to satisfy the borrow checker isn't always the best solution. Learn borrowing patterns first.

Expecting Garbage Collection

Coming from GC languages, you need to think about ownership and lifetimes explicitly.

Ignoring the Type System

Rust's type system is powerful. Use Option and Result instead of panicking.

Development Environment

Editor Support

Toolchain

Community and Resources

Official Resources

Community

Checks for Understanding

  1. What are the three main goals of Rust? (Memory safety, performance, concurrency)
  2. How does Rust achieve memory safety without garbage collection? (Ownership system)
  3. What is a zero-cost abstraction? (High-level features that compile to efficient code)
  4. Name three common use cases for Rust. (Systems programming, web services, CLI tools)
  5. What is the difference between Result and Option? (Result for errors, Option for nullable values)

Answers

  1. Memory safety, performance, and concurrency
  2. Through its ownership system that tracks memory usage at compile time
  3. A feature that provides convenience without runtime cost
  4. Systems programming, web development, command-line tools, blockchain, etc.
  5. Result handles success/error cases, Option handles some/none cases

Next →