Ownership & Borrow Checker
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!

Let's be more specific
1. Moving Ownership
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.â
fn main() {
let toy = String::from("Toy car"); // `toy` owns the string
let new_owner = toy; // `toy` is moved to `new_owner`
// println!("{}", toy); // Error! `toy` no longer owns the value
println!("{}", new_owner); // This works since `new_owner` is the current owner
}2. Borrowing Mutably (or with Permission to Change)
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.
fn main() {
let mut toy = String::from("Toy car");
// `toy` is borrowed mutably by `decorate`
decorate(&mut toy);
println!("{}", toy); // Prints: "Toy car with stickers"
}
fn decorate(toy: &mut String) {
toy.push_str(" with stickers"); // Modify the borrowed `toy`
}3. Borrowing Immutably (Just to Look)
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.
fn main() {
let toy = String::from("Toy car");
let look1 = &toy; // Immutable borrow
let look2 = &toy; // Another immutable borrow
println!("Look 1: {}", look1); // Prints: "Look 1: Toy car"
println!("Look 2: {}", look2); // Prints: "Look 2: Toy car"
// `toy.push_str(" new")`; // Error! Cannot modify `toy` while it is immutably borrowed
}4. When the Toy Goes Out of Scope (Cleaning Up)
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.
fn main() {
let toy = String::from("Toy car"); // `toy` owns the string
{
let _borrowed_toy = &toy; // `_borrowed_toy` borrows `toy`
println!("Inside scope: {}", _borrowed_toy); // Works within this inner scope
} // `_borrowed_toy` goes out of scope here
println!("Outside scope: {}", toy); // Still accessible since `toy` is still in scope
} // `toy` goes out of scope and is automatically cleaned upLast updated