Module 2 • Lesson 7

Understanding Ownership

📚 10 min read 💻 Free Course 🦀 nixus.pro

Ownership: Rust's Killer Feature

Ownership is the most unique concept in Rust - the reason it achieves memory safety without garbage collection. Three rules govern ownership, enforced entirely at compile time with zero runtime cost.

The Three Rules

fn main() {
    let s = String::from("hello"); // s owns "hello"
    println!("{}", s);
} // s goes out of scope -> drop() called -> memory freed

fn scope_demo() {
    let x = 5;  // x is valid from here
    {
        let y = 10; // y valid here
        println!("{} {}", x, y);
    } // y dropped here
    // println!("{}", y); // ERROR: y not in scope
    println!("{}", x); // x still valid
}

Move Semantics

When you assign a heap-allocated value to another variable, ownership moves. The original variable becomes invalid:

fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // s1 MOVED to s2

    // println!("{}", s1); // ERROR: s1 was moved!
    println!("{}", s2);    // OK: s2 is the owner

    // Primitives are COPIED, not moved:
    let x = 5;
    let y = x;
    println!("{} {}", x, y); // Both valid - i32 implements Copy

    // Explicit clone for heap types:
    let a = String::from("hello");
    let b = a.clone();  // Deep copy - new heap allocation
    println!("{} {}", a, b); // Both valid
}

// Ownership through functions:
fn takes_ownership(s: String) {
    println!("{}", s);
} // s dropped here

fn returns_ownership() -> String {
    String::from("returned") // Ownership moves to caller
}

fn main2() {
    let s = String::from("hi");
    takes_ownership(s); // s moved in
    // s is no longer valid here

    let r = returns_ownership();
    println!("{}", r); // r owns the returned String
}

🎯 Practice

  1. Create a String, move it to a function. Try to use the original - read the compiler error.
  2. Fix the above by returning the String from the function
  3. Demonstrate that i32 can be used after assignment (Copy semantics)
  4. Use clone() to allow using both the original and a copy

🎉 Key Takeaways