Pybites Logo Rust Platform

Ownership and Borrowing

Medium +3 pts

🎯 In Python, you never think about who "owns" a value:

def label(item):
    return f"Product: {item}"

item = "Laptop"
tag = label(item)
print(item)  # still works — item wasn't consumed

Every variable is a reference to a garbage-collected object. Pass it to a function, return it, store it — the GC handles cleanup.

Rust has no garbage collector. Instead, it uses ownership — every value has exactly one owner, and when the owner goes out of scope, the value is dropped (freed). When you pass a value to a function, you transfer ownership (a "move"):

fn announce(item: String) -> String {
    format!("Now selling: {}!", item)
}

let item = String::from("Laptop");
let msg = announce(item);
// item is gone — it was moved into announce()

Borrowing: …

Login to see the full exercise.