Chapter 21: Rust Borrowing

Rust Borrowing like we’re sitting together with a laptop and some filter coffee: slowly, clearly, with many small examples you can copy-paste and run, everyday analogies from Hyderabad life, and building from very simple → common patterns → tricky cases.

1. What is Borrowing? (The Simple Idea)

Borrowing = temporarily accessing data without taking ownership of it.

You “borrow” the data for a while, use it (read or write), then give it back — the original owner still owns it after you’re done.

Rust has two kinds of borrowing:

Kind Syntax Can read? Can write/mutate? How many at the same time? Analogy (Hyderabad style)
Immutable borrow &T Yes No Many (unlimited) Many people take photos of your biryani plate
Mutable borrow &mut T Yes Yes Exactly one at a time Only one friend is allowed to eat from your plate

These two rules are enforced at compile time by the borrow checker — no runtime cost.

2. Immutable Borrowing (&) – Read-Only Access

Most common borrowing — safe, cheap, unlimited.

Rust

Key points:

  • &s = create an immutable reference (borrow)
  • Function gets a view — does not own or move anything
  • You can have as many immutable borrows as you want at the same time
  • Original owner (s) still fully owns the data — can keep using it

3. Mutable Borrowing (&mut) – Exclusive Write Access

Only one mutable borrow allowed at any moment.

Rust

But this fails:

Rust

Compiler error (very clear in 2026):

text

→ Prevents data races even in single-threaded code

4. The Famous Borrowing Rules (Official + Explained)

Rust enforces these at the same time:

  1. At any given time, you can have either:
    • Any number of immutable borrows (&T)
    • OR exactly one mutable borrow (&mut T)
  2. References must always be valid (cannot outlive the data they point to → dangling prevented)
Rust

→ The borrow checker tracks until last use, not until end of scope

5. Common Pattern: &str vs &String in Functions

Rust

Always prefer &str in function parameters — more flexible, zero-cost coercion from &String

6. Dangling References – Prevented at Compile Time

Rust

Error: “borrowed value does not live long enough”

7. Borrowing in Structs & Methods (Quick Preview)

Rust

Teacher Summary – The Big Picture

Rust Borrowing =

  • Temporarily access data without moving ownership
  • &T → read-only, many allowed simultaneously
  • &mut T → read-write, only one allowed at a time
  • Prevents data races, use-after-free, dangling pointers — all at compile time
  • Key to writing safe, efficient code without GC or manual memory management

Analogy for Hyderabad: Your family owns one scooter (the String).

  • Immutable borrow = many friends take selfies with the scooter (everyone can look)
  • Mutable borrow = only one friend is allowed to ride it (exclusive control)
  • You can’t let someone ride it while others are taking photos at the same time — Rust stops you before accident happens.

Practice today:

  1. Write function that borrows &str and returns length + first 5 chars
  2. Create mutable borrow conflict → read compiler message
  3. Try to mutate through &String (should fail)
  4. Use &mut to append city name to a greeting

Ready for the next big piece?

  • Lifetimes (they explain how long borrows are allowed to live)?
  • Borrow checker error messages in detail?
  • Or structs + borrowing inside methods?

Just tell me — your Rust teacher is right here with you! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *