Skip to main content
eLearner.app

Rust Course

Cheatsheet

A quick reference — the essential syntax of the Rust language on a single page. Press Ctrl/Cmd + P to print it.

Rust · Cheatsheet — eLearner.app

Variables and Mutability

  • Declarations and Shadowing

    let x = 5;              // Immutable by default
    let mut y = 10;         // Mutable (mut)
    const MAX: u32 = 100;   // Constant (type annotation required)
    let x = x + 1;          // Shadowing (re-declaring the same name)

    Variables in Rust are immutable by default to ensure safety and concurrency.

Primitive Types

  • Tuples and Arrays

    // Tuple (fixed size, heterogeneous types)
    let tuple: (i32, f64, char) = (500, 6.4, 'z');
    let first = tuple.0; // Index access
    
    // Array (fixed size, homogeneous types)
    let array = [1, 2, 3, 4, 5];
    let second = array[1]; // Index access

    Array indices start at 0. Rust checks array bounds access at runtime, causing a panic if out of bounds.

Control Flow

  • Conditionals and if Expressions

    // Classic if/else
    if number < 5 {
        println!("Small");
    } else {
        println!("Big");
    }
    
    // if used as an expression to assign value
    let condition = true;
    let number = if condition { 5 } else { 6 };
  • Loops (loop, while, for)

    // loop (infinite loop)
    loop {
        break;
    }
    
    // conditional while
    while number != 0 {
        number -= 1;
    }
    
    // for loop over a range
    for number in 1..4 { // From 1 to 3
        println!("{}", number);
    }

Pattern Matching

  • match, Option and Result

    // Value matching
    match number {
        1 => println!("One"),
        2 => println!("Two"),
        _ => println!("Other"), // Must exhaustively match all possibilities
    }
    
    // Matching Option
    match x {
        Some(val) => println!("Found: {}", val),
        None => println!("No value"),
    }
    
    // Matching Result
    match res {
        Ok(val) => println!("Success: {}", val),
        Err(e) => println!("Error: {}", e),
    }

Ownership and Borrowing

  • Move and References (&)

    let s1 = String::from("hello");
    let s2 = s1; // Ownership is moved (s1 is no longer usable!)
    
    let len = calculate_length(&s2); // Pass by immutable reference (&)
    
    let mut s3 = String::from("hello");
    change(&mut s3); // Pass by mutable reference (&mut)

    Borrowing Rules: you can have any number of immutable references (&T), OR exactly one mutable reference (&mut T) at a time.

Structs and Methods

  • Definition and impl

    struct Rectangle {
        width: u32,
        height: u32,
    }
    
    impl Rectangle {
        // Instance method (takes &self)
        fn area(&self) -> u32 {
            self.width * self.height
        }
    
        // Associated function (constructor)
        fn square(size: u32) -> Rectangle {
            Rectangle { width: size, height: size }
        }
    }
    
    // Usage
    let rect = Rectangle::square(10); // Associated function call
    let a = rect.area();             // Method call
eLearner.app · Rust Course · cheatsheet generated from lesson content.