Ownership & Borrow Checker
Last updated
Last updated
Ownership in Rust could be a hard topic to understand and apply it in real code. Hence, let’s imagine Rust ownership using toys!
Imagine you have a toy car. In Rust, only one person can own that toy car at a time. This means only one person can hold it, play with it, and take care of it.
Ownership Rule: If you give your toy car to your friend, now they are the owner, not you. You can't play with it anymore unless they give it back. This way, there's only ever one owner at a time—this prevents anyone from accidentally breaking it by both trying to use it at once.
Borrowing Rule: Now, let's say you want to let your friend borrow your toy for a while without giving it to them completely. You can say, "Here, you can play with my toy, but I still own it." In Rust, this is called "borrowing." Your friend can play with the toy, but they know it still belongs to you and has to give it back soon. This keeps the toy safe because it always has one true owner.
No Double-Owners Rule: Lastly, only one person can borrow the toy at a time to make sure it doesn’t get mixed up or broken. But if everyone just wants to look at the toy without touching it, several friends can peek at it at the same time. In Rust, these are called "immutable references."
So, Rust's ownership is like toy rules: only one owner, borrowing is temporary, and no fighting over the toy!
Imagine you’re holding your toy car, and you decide to give it to your friend Sam. When you hand the toy over, you’re not just sharing—you’re transferring full ownership to Sam. Now, only Sam can play with that toy, and you can’t touch it anymore. In Rust, we call this “moving” ownership. It’s like saying, “Hey, Sam, this toy car is yours now. I’ll get my own toy.”
Now, let’s say you’ve kept your toy car but let your friend Jamie borrow it. Jamie asks if they can change it by adding cool stickers. You decide, "Okay, but remember, I still own it!" In Rust, this is a mutable borrow. Jamie can make changes, but only because you allowed it temporarily. Once Jamie is done, they give it back, and you have your newly decorated toy car.
In Rust, you can only let one person at a time make changes with a mutable borrow. This ensures that there’s no confusion or conflict over who is modifying the toy.
Now, let’s say a few of your friends just want to look at the toy, not play with it or add stickers. They ask, and you say, “Sure, you can all look at it, but don’t touch or change anything!” This is called an immutable borrow. You can let multiple friends look at it at once because they’re not changing anything. In Rust, this allows for several immutable borrows at the same time.
Finally, imagine it’s the end of the day, and you have to go home. You take your toy car with you, but if you forget it at your friend’s house, their parent might pick it up and put it away. In Rust, this is like when a value goes out of scope. When you’re done with a variable (or toy), Rust automatically “cleans it up” for you so there’s no leftover mess.
In programming terms, this cleanup is called dropping, and Rust does it automatically when the toy (or variable) is no longer needed.