Understanding Rust's Ownership Model

JITPOMIJITPOMI
3 min read

Understanding Rust's ownership system can be tough at first, but once you think of variables as bank cards and values as bank accounts, everything starts to make sense.


1. πŸ’³ Ownership (Move)

let card_one = String::from("Bank Account: PIN 1234");
let card_two = card_one; // card_one is invalidated (dropped), now card_two owns the account
println!("{}", card_one); // ❌ ERROR! 
// If the teller borrows card_one to try and print your bank details after you dropped it,
// they'll find the card is invalid.
println!("{}", card_two); // βœ…
// If they borrow card_two however, they'll successfully print your bank info.

πŸ‘‰ Only one card can own the account at a time. When ownership is moved, the old card becomes useless.


2. πŸ“· Cloning

let card_one = String::from("Bank Account");
let card_two = card_one.clone(); // make a copy of both the card and the account
println!("{} and {}", card_one, card_two); // βœ…

πŸ‘‰ Now we have two cards each pointing to its own separate account. The accounts just happen to hold similar info, but they are distinct items.


3. πŸ•΅οΈ Immutable Borrowing (Read-Only)

let card_one = String::from("Bank Account");
let card_copy_one = &card_one;  // borrow and read as much as you want as long as you can't change
let card_copy_two = &card_one; // borrow and read as much as you want as long as you can't change
println!("{}, {}", card_copy_one, card_copy_two ); // βœ…

πŸ‘‰ Multiple read-only copies are fine. No one can change anything, but everyone can check the balance.


4. ✍️ Mutable Borrowing (Exclusive Write Access)

Let's say you want to change your account info:

let mut card_one = String::from("Bank Account");
// A financial assistant temporarily borrows your card, but this time with edit rights (mutable borrow)
let editor = &mut card_one;
println!("{}", card_one); // ❌
// If the financial assistant is still working with the card, you're not allowed to use it.
editor.push_str(" - PIN Updated"); // they update your PIN

But when the edit is complete: You are free to use it

let mut card_one = String::from("Bank Account");
let editor = &mut card_one;
editor.push_str(" - PIN Updated"); // βœ… Editor finished
println!("{}", card_one); // βœ… Back to the owner

Note: After the financial assistant is done changing your account and handed over ownership to you, they are not allowed to use their mutable borrow to read or update your bank account anymore.

let mut card_one = String::from("Bank Account");
let editor = &mut card_one;
editor.push_str(" - PIN Updated"); // βœ… Editor finished
println!("{}", card_one); // βœ… Back to the owner
println!("{}", editor); // ❌
// Once the mutable borrow is done, you can't keep using the editor to access the account.

πŸ‘‰ Only one mutable borrow is allowed at a time. And during that time, the owner can't access the card.


Summary

  • Only one owner (card) at a time.

  • You can clone to make a copy (new card + new account).

  • You can borrow immutably multiple times to read, but not change.

  • You can borrow mutably only once, exclusively, to edit.

Rust is like a strict bank that always knows who's holding the card, and won’t let you mess up account access rules.

🧠 Once you understand the card/account model, you understand Rust’s safety rules.

0
Subscribe to my newsletter

Read articles from JITPOMI directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

JITPOMI
JITPOMI

β€œWe spice up your hustle!” At JITPOMI, we're all about empowering your grind through cutting-edge tech solutions β€” whether it’s in academia, trade, finance, media, or faith. We exist to make you better by helping you reach your business goals and exceed expectations through collaboration and co-creation.