πŸ“±
World 4 Β· The Ownership Superpower

One Owner at a Time

Here’s the idea Rust is most famous for: ownership. It sounds fancy, but it’s something you already understand from real life. Think about your phone β€” you own it. Right now, you’re the one holding it. In Rust, every value works the same way: it has one owner at a time. πŸ“±

The Big Idea In Rust, every value has exactly one owner β€” a variable that holds it. When that owner goes away, Rust automatically cleans the value up. No leftover mess, no memory leaks.

One owner at a time

When you write let message = String::from("Hello!");, the variable message becomes the owner of that text. A String is a piece of text the program stores in memory.

Think of it like this… The variable is the person, and the value is the phone. One person holds one phone β€” so it's always crystal clear who's responsible for it.

Giving it away = β€œmoving”

What happens if you hand your phone to a friend? Now they have it, and you don’t. Rust calls this a move. After you move a value to a new owner, the old name can’t use it anymore.

If you tried to use the old name, Rust would stop you at compile time and explain that the value was moved. That’s Rust protecting you from a whole class of bugs. πŸ›‘οΈ

When we passed message into send, ownership moved inside the function. After that, message no longer owns anything, so we don’t use it again.

Numbers are different

Small, simple values like numbers don’t get moved β€” they get copied. A number is more like a fact you can share: if you tell a friend your score, you both know it. So both variables keep working.

Ferris says: Larger values like String get moved. Small ones like numbers get copied. Either way, Rust keeps everything safe. πŸ¦€
Try this! In the first box, change "Hello!" to your own message and press β–Ά Run. Watch the value travel into the function.

Quick quiz

After you move a String to a new owner, can you still use the old name?

Correct! A move hands the value to a new owner, so the old name can no longer use it. πŸ“±

You learned… Every value has one owner, handing a String to a new owner is a move (the old name stops working), and small values like numbers are copied instead. Next up: how to lend a value to a function and get it back β€” that's borrowing! 🀝