IS4010: AI-Enhanced Application Development

Week 11: The Soul of Rust

Brandon M. Greenwell

Session 1: Ownership & the borrow checker

Rust’s core challenge: memory management

  • A central problem in systems programming is managing the computer’s memory.
  • Python uses a garbage collector that runs in the background to clean up memory that’s no longer being used. This is convenient but can impact performance.
  • Other languages like C/C++ require manual memory management, which is fast but makes it very easy to make mistakes that lead to bugs and security vulnerabilities.
  • Rust introduces a third, unique approach: ownership, which provides memory safety without a garbage collector.

The ownership rules

  • This is the most critical concept in Rust. It is enforced by the compiler at compile time.
  • There are three core rules:
    1. Each value in Rust has a variable that’s called its owner.
    2. There can only be one owner at a time.
    3. When the owner goes out of scope, the value is dropped (its memory is freed).
  • This system prevents a whole class of common programming errors.

The borrow checker

  • The borrow checker is the part of the Rust compiler that enforces the ownership rules.
  • If we want to use a value without taking ownership, we can create a reference to it. This is called borrowing.
  • There are two rules for borrowing:
    1. You can have any number of immutable references (&T) to a piece of data at once.
    2. You can only have one mutable reference (&mut T) at a time.
  • This prevents “data races,” where multiple parts of the code try to change the same data simultaneously.

Introducing the lab (part 1)

  • The first part of our lab this week is “the borrow checker game”.
  • I will provide you with several Rust code snippets that intentionally violate the ownership and borrowing rules.
  • Your task is to work with the compiler’s error messages to fix the code. This is a fundamental skill for all Rust developers.

Session 2: Structs & enums

Creating custom types with structs

  • A struct is a way to create a custom data type by grouping related values together into a single, meaningful unit.
  • They are similar to python classes, but by default, they only contain data fields, not methods.
  • You define the struct with named fields and their types, then create an instance of it.
struct User {
    username: String,
    email: String,
    active: bool,
}

fn main() {
    let user1 = User {
        email: String::from("ada@example.com"),
        username: String::from("ada"),
        active: true,
    };
    
    println!("Username: {}", user1.username);
}

The power of enums

  • An enum (or enumeration) is a way to define a custom type by listing all of its possible variants.
  • They are perfect for modeling a value that can only be one of a finite set of possibilities.
  • The match keyword is Rust’s powerful control flow operator for handling enums. It forces you to handle every possible variant, preventing bugs.
enum WebEvent {
    PageLoad,
    KeyPress(char),
    Click { x: i64, y: i64 },
}

fn handle_event(event: WebEvent) {
    match event {
        WebEvent::PageLoad => println!("Page loaded"),
        WebEvent::KeyPress(c) => println!("Pressed '{}'.", c),
        WebEvent::Click { x, y } => println!("Clicked at x={}, y={}.", x, y),
    }
}

AI-assisted data modeling

  • Choosing the right combination of structs and enums is a key part of designing Rust applications.
  • Your AI partner can be very helpful here. You can describe the data you need to represent and ask for suggestions.
  • Example prompt: “i need to model a blog post in rust. it can be in a draft, reviewed, or published state. it should have a title and content. what combination of structs and enums should i use?”

Introducing lab 11

  • For the second part of the lab, you will model the data for a simple application using the structs and enums you just learned.
  • This will give you practice creating and interacting with your own custom data types.
  • Reminder: Your final project proposals are due at the end of this week. Please submit them so I can provide feedback.