II
🔗 Ownership & Borrowing
What is Ownership?
Concept
Every value in Rust has a single owner. When the owner goes out of scope, the value is dropped and memory is freed. This is Rust's core memory model — no garbage collector needed. The ownership rules are: • Each value has exactly one owner • There can only be one owner at a time • When the owner goes out of scope, the value is dropped
example.rs
fn main() {
    let s = String::from("hello"); // s owns the String
    
    {
        let s2 = String::from("world"); // s2 owns this String
        println!("{}", s2); // s2 valid here
    } // s2 dropped here — memory freed
    
    println!("{}", s); // s still valid
} // s dropped here
Quick Check

What happens when a variable goes out of scope?

1 / 3