Rust 101

🦀 Rust Crash Course

Getting Started with Rust

Learn the fundamentals of ownership, memory safety, and performance in Rust 2021 edition.

Rust Core Concepts

Master the principles that make Rust a systems programming language of the future.

Ownership System

Rust's ownership model ensures memory safety without a garbage collector, with rules enforced at compile time.

fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // s1 is no longer valid
    println!("{}", s2);
}

Borrow Checker

The borrow checker enforces reference validity rules to prevent dangling pointers and data races.

fn main() {
    let mut s = String::from("hello");
    let s2 = &s;  // immutable borrow
    // s.push_str("!"); // error: cannot borrow `s` as mutable because it is also borrowed as immutable
    println!("{}", s2);
}

Memory Safety

Rust ensures memory safety without garbage collection through compile-time correctness guarantees and zero-cost abstractions.

  • No runtime GC
  • Stack allocation
  • Compile-time optimizations

Why Rust?

Rust combines the low-level control of C++ with the safety guarantees of modern languages. It's perfect for systems programming, web assembly, and embedded systems.

Next Section →

Advanced Rust Concepts

Take your Rust knowledge to the next level with pattern matching, lifetimes, and macros.

Pattern Matching

Rust's powerful pattern matching system allows you to destructure complex data types with precise control flow.

fn main() {
    let x = 5;
    
    match x {
        1 => println!("one"),
        2 => println!("two"),
        3 => println!("three"),
        4 => println!("four"),
        5 => println!("five"),
        _ => println!("other"),
    }
}

Lifetimes

Explicit lifetime annotations allow precise control over how long references to data are valid.

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

fn main() {
    let string1 = String::from("long string is long");
    let string2 = "abc";
    
    let result = longest(&string1, &string2);
    println!("The longest string is {}", result);
}

Macros

Rust's macro system enables powerful metaprogramming capabilities with hygienic macro expansion.

macro_rules! create_function {
    ($func_name:ident) => {
        fn $func_name() {
            println!("Hello from {}!", stringify!($func_name));
        }
    };
}

create_function!(hello); // creates function hello()

fn main() {
    hello();
}

Recommended Learning Resources

The Rust Book

The official Rust documentation covering all aspects of the language with examples and exercises.

Read Docs

Rust By Examples

Interactive tutorial with 150+ working code examples to explore in your browser.

Explore Examples

Rustlings

Collection of hands-on exercises to teach you Rust fundamentals through practice.

Practice Now