~/
Theme

🦍 Rust Ownership & Lifetimes (For Monkeys!)

🍌 What is Ownership? (Rust’s Biggest Rule!)

Rust does not have garbage collection. Instead, it has ownership rules to manage memory automatically!

🏑 Rule #1: Every Value Has One Owner

fn main() {
    let banana = String::from("🍌"); // banana owns this String
    let monkey = banana; // Ownership MOVED to monkey!
    // println!("{}", banana); // ❌ ERROR! banana lost ownership!
    println!("{}", monkey); // βœ… Works!
}

When banana gives ownership to monkey, it loses access to the banana! 🐡

🏑 Rule #2: When the Owner Dies, the Value is Dropped

fn main() {
    {
        let food = String::from("🍎"); // food is created
        println!("Monkey eats: {}", food);
    } // food goes out of scope, and is DROPPED πŸ—‘οΈ
    // println!("{}", food); // ❌ ERROR! food is GONE!
}

When food goes out of scope, Rust automatically cleans it up! 🧹

πŸ”„ Borrowing (Sharing Bananas!)

Monkeys don’t like sharing, but Rust allows borrowing instead of moving ownership!

🏑 Rule #3: You Can Borrow Without Taking Ownership

fn eat(banana: &String) { // Borrowing (not moving)
    println!("Monkey eats: {}", banana);
}

fn main() {
    let banana = String::from("🍌");
    eat(&banana); // βœ… Works! banana is still owned by main
    println!("Banana is still here: {}", banana);
}

Borrowing (&) allows you to use a value without owning it!

🚫 Rule #4: No Mutability Conflicts!

You can:

    1. Have multiple immutable references (readonly 🧊)
    1. OR only one mutable reference (write access ✏️)
fn main() {
    let mut banana = String::from("🍌");
    let monkey1 = &banana; // Immutable borrow
    let monkey2 = &banana; // Another immutable borrow
    println!("Monkeys see: {} and {}", monkey1, monkey2); // βœ… Works!

    // let monkey3 = &mut banana; // ❌ ERROR! Can't mutate while others read!
}
fn main() {
    let mut banana = String::from("🍌");
    let monkey = &mut banana; // βœ… Allowed, only ONE mutable borrow
    monkey.push_str("🍌"); // Modify banana!
    println!("Monkey added more bananas: {}", monkey);
}

⏳ Lifetimes (How Long Can You Borrow?)

Sometimes monkeys borrow bananas for too long! Rust checks lifetimes to make sure bananas don’t disappear while being borrowed! 🍌⏳

🏑 Rule #5: Lifetimes Define How Long a Borrow is Valid

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

fn main() {
    let banana = String::from("🍌🍌🍌");
    let apple = String::from("🍎");
    let best = longest(&banana, &apple);
    println!("Best fruit: {}", best);
}

Here, longest<'a> means both inputs and output share the same lifetime.

❌ What Happens When Lifetimes Are Wrong?

fn bad() -> &String { // ❌ ERROR! No lifetime!
    let banana = String::from("🍌");
    &banana // ❌ banana is DROPPED here!
}

The banana disappears before it can be returned! 🚨

🏁 Summary (Monkey Wisdom 🍌)

  • βœ” Ownership moves when assigned to another variable ➑️
  • βœ” Every value has one owner 🏑
  • βœ” Borrowing (&) lets you use without owning πŸ”„
  • βœ” Multiple immutable or one mutable borrow allowed 🚦
  • βœ” Lifetimes ensure borrowed values live long enough ⏳

Now go write some Rust without dropping your bananas! πŸ΅πŸš€