Introduction to Rust

Welcome to your first lesson in Rust programming! In this introduction, we'll cover what Rust is, why it's unique, and what makes it an excellent choice for modern software development.

What is Rust?

Rust is a systems programming language that focuses on safety, concurrency, and performance. It was originally developed by Mozilla Research and has since become one of the most loved programming languages according to Stack Overflow's annual developer survey.

Key Features

Memory Safety Without Garbage Collection

fn main() {
    // Variables are immutable by default
    let name = "Rust";

    // Use mut to make a variable mutable
    let mut count = 0;
    count += 1;

    println!("Learning {} - Count: {}", name, count);
}

Zero-Cost Abstractions

Rust provides high-level features that compile down to efficient low-level code:

// Vector (dynamic array) with type inference
let numbers = vec![1, 2, 3, 4, 5];

// Functional programming features
let doubled: Vec<i32> = numbers
    .iter()
    .map(|x| x * 2)
    .collect();

Why Learn Rust?

  1. Safety: Rust's ownership system prevents common bugs at compile-time
  2. Performance: Zero-cost abstractions and no garbage collector
  3. Modern Tooling: Excellent package manager (Cargo) and documentation
  4. Growing Ecosystem: Active community and increasing adoption

Getting Started

To start coding in Rust, you'll need to:

  1. Install Rust using rustup
  2. Set up your development environment
  3. Learn the basic syntax
  4. Understand Rust's ownership model

We'll cover each of these topics in detail in the upcoming lessons.

Interactive Playground

Practice what you've learned! Edit and run the code below to experiment with Rust.

fn main() {
    // Try changing these values!
    let language = "Rust";
    let year = 2024;

    // String formatting in Rust
    println!("Learning {} in {}!", language, year);

    // Create a vector and double its values
    let numbers = vec![1, 2, 3, 4, 5];
    let doubled: Vec<i32> = numbers
        .iter()
        .map(|x| x * 2)
        .collect();

    println!("Original numbers: {:?}", numbers);
    println!("Doubled numbers: {:?}", doubled);
}

Try these exercises:

  1. Change the language variable to your favorite programming language
  2. Add more numbers to the vector
  3. Instead of doubling, try multiplying the numbers by 3
  4. Add a new variable and include it in the first println statement

Pro Tip: The best way to learn Rust is by practicing. Try modifying the code examples above and experiment with them in the Rust Playground.