DEV Community

Cover image for Cozy Bytes 🍺 | Rust::Who Owns the Cat? πŸˆπŸ¦€
nyaomaru for Brain Buffering

Posted on Edited on AI-assisted

Cozy Bytes 🍺 | Rust::Who Owns the Cat? πŸˆπŸ¦€

Explains memory rules using friendly felines

Hoi hoi! 😸

Happy Friday! Let's learn a tiny bit of Rust! 😸🍻

No complicated architecture. No giant diagrams. Just a cat, a crab, and a little bit of ownership.

Grab a beer! 🍺


🐈 Ownership::This Is My Cat!

Rust has three simple ownership rules:

  • Every value has an owner.
  • There can only be one owner at a time.
  • When the owner goes out of scope, the value is dropped.

Let's adopt a cat!

let a = String::from("cat");
let b = a;

println!("{b}");
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Move::Wait... Where's My Cat?!

What happens if we try to use a again?

println!("{a}"); // ❌ Compile error!
Enter fullscreen mode Exit fullscreen mode

WHAT?! WHERE IS MY CAT?! πŸ™€

Ownership moved from a to b.

Sorry, a. That's not your cat anymore. 😿

Rust remembers. Rust never forgets. πŸ¦€

Small note: This behavior applies to non-Copy types like String. Simple types such as i32 implement Copy, so let b = a; copies the value and a can still be used. 😸


🀝 Borrowing::Can I Pet Your Cat?

What if we just want to borrow a value without taking ownership?

We can use a reference (&).

let a = String::from("cat");
let b = &a;

println!("{b}");
println!("{a}");
Enter fullscreen mode Exit fullscreen mode

WOW! Both work! 😸

a still owns the value. b is just borrowing it.

You can pet my cat, but you can't take it home. 😼


✏️ MutableBorrowing::The Cats Are Multiplying!

A shared reference (&) doesn't let us modify the borrowed String.

But a mutable reference (&mut) does!

let mut a = String::from("cat");

let b = &mut a;
b.push_str(" and another cat");

println!("{a}");
Enter fullscreen mode Exit fullscreen mode

Output πŸ‘‡

cat and another cat
Enter fullscreen mode Exit fullscreen mode

Oh no.

The cats are multiplying. 🐈🐈


πŸ¦€ BorrowChecker::One Human at a Time!

Multiple humans can pet the same cat.

let cat = String::from("cat");

let a = &cat;
let b = &cat;

println!("{a} {b}"); // βœ…
Enter fullscreen mode Exit fullscreen mode

Everyone is happy. The cat gets extra attention. 😸

But what if two humans try to brush the same cat at once?

let mut cat = String::from("cat");

let a = &mut cat;
let b = &mut cat; // ❌ Compile error!

a.push_str(" is fluffy");
b.push_str(" is SUPER fluffy");
Enter fullscreen mode Exit fullscreen mode

Rust::ABSOLUTELY NOT. πŸ¦€πŸ’’

So here's the rule:

  • & β€” Many humans can borrow the cat for reading.
  • &mut β€” Only one human can borrow the cat for modification, with no other active borrows.

Want to brush the cat? Wait until everyone else is done borrowing it!

The crab is just trying to keep our cats safe. πŸˆπŸ’š


πŸͺ Summary::One Little Byte at a Time

  • Ownership -> This is my cat.
  • Move -> It's your cat now.
  • & -> You can pet my cat.
  • &mut -> You can brush my cat, but one human at a time.
  • BorrowChecker -> The angry crab protecting our cats. πŸ¦€

That's it for today's Cozy Bytes!

Rust looks scary sometimes, but we don't have to learn everything at once.

One little byte at a time. πŸ˜ΈπŸ’š

Happy Friday! 🍻

Top comments (23)

Collapse
 
antonioprosperi2svg profile image
Antonio •

Thanks! This is where I finally grasped the concept of "ownership"! πŸˆπŸ¦€

So, if I understand correctly: without &, the "cat" moves to b, and trying to use a again causes a compilation error. With &, however, the cat is merely "lent out," so a remains usable.

In JS, writing let b = a with an object simply means sharing itβ€”both can modify it without the other even noticing. Rust’s "crab," on the other hand, puts a stop to that before the program even runs. 😹

One byte at a timeβ€”thanks! πŸ’š

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

Exactly! πŸ˜ΈπŸˆπŸ¦€

Without &, ownership of the String moves to b, so a can’t use it anymore. With &, b only borrows it, and a stays the owner.

And yeah, coming from JavaScript, that difference feels especially interesting because objects are normally just shared by reference.

Glad the cat helped ownership finally click! πŸ˜ΉπŸ’š

One byte at a time!

Collapse
 
xulingfeng profile image
xulingfeng •

This makes me want to get a cat too πŸ˜‚

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

Careful... that’s how it starts. 😹🐈
First you learn Rust ownership.
Then suddenly a cat owns you. πŸ™€

Collapse
 
mestonecold profile image
Shruti Shahi Brain Buffering •

lolπŸ˜‚

Collapse
 
csm18 profile image
csm •

And we can pass a mutable ref to a function and then after the function call, that is, after the function is done running, we can again have immutable ref.

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

That makes sense. Rust just wants us to take turns with the cat. πŸˆπŸ¦€
Thanks for the extra detail! πŸ’š

Collapse
 
mestonecold profile image
Shruti Shahi Brain Buffering • • Edited

@nyaomaru arigatto for the first post for Brain Buffering, and congratulations- it blew up!😺πŸ₯‚
And this...😭

The cats are multiplying. 🐈🐈

I wish cats could multiply like that; I'd have a thousandπŸ˜‚
This was a really creative one!✨

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

YAY!! πŸ˜ΈπŸ’š

Douitashimashite!
I’m really happy the first Brain Buffering post got such a nice response. πŸˆπŸ¦€βœ¨

And yes, if cats could multiply that easily, I think Necoz would have a serious population problem by tomorrow. 😹🐈🐈🐈

Glad you enjoyed it! πŸ₯‚

Collapse
 
mestonecold profile image
Shruti Shahi Brain Buffering •

haha, and the fact that the engagement is still growing! Sugoiβœ¨πŸ˜ΊπŸ’“
Necoz is a whole mood ngl and I love itπŸ˜­πŸ˜‚

Thread Thread
 
nyaomaru profile image
nyaomaru Brain Buffering •

YAY!! πŸ˜ΈπŸ’š

Please enjoy Necoz as much as you want!
You’re one of the cats too now. 🐈✨

Wander around, have fun, make things, disappear for a nap, come back later...
Cat rules. 😹

Thread Thread
 
mestonecold profile image
Shruti Shahi Brain Buffering •

Hai! 😸🩡

Collapse
 
rosana_e4663528aeaba0b05b profile image
Rosana •

This is such a delightful way to introduce one of Rust’s most important concepts. The cat analogy works because it turns an abstract memory-management rule into something intuitive: ownership isn't simply about who can access a value, but who is ultimately responsible for it. That distinction is at the heart of what makes Rust different, and your examples make it much less intimidating for beginners.

I particularly like how you move from ownership to borrowing. The idea that someone can pet your cat without taking it home captures the difference between owning a value and referencing it beautifully. It's also a useful way to explain why borrowing is so powerful: we can share access to data without unnecessarily transferring responsibility or creating copies. That is a fundamental concept that becomes increasingly important as Rust programs grow more complex.

The mutable borrowing example is especially valuable because it introduces the reasoning behind Rust's restrictions rather than presenting the borrow checker as an arbitrary obstacle. Allowing multiple readers while restricting simultaneous mutable access helps prevent situations where different parts of a program unexpectedly interfere with one another. In other words, Rust is enforcing rules that make potentially dangerous states harder to express in the first place.

One additional point that might help readers as they progress is that these rules aren't just about the number of references. Their lifetimes and scopes matter, too. A shared reference can coexist with other shared references, while a mutable reference requires exclusive access for the duration of its use. Rust's non-lexical lifetimes also mean that a borrow can end after its last use, even before the enclosing scope finishes. That detail often helps beginners understand why some borrowing patterns compile while others don't.

I'd also emphasize that ownership isn't a limitation designed to make programming harder; it's a way of moving important correctness guarantees into compile time. Instead of discovering certain memory-safety problems through unpredictable runtime behavior, developers get feedback from the compiler before the program runs. Once that mindset clicks, the borrow checker starts to feel less like an angry crab and more like a very strict colleague who catches problems before they reach production. πŸ˜„

What makes this tutorial particularly effective is its pacing. You introduce one idea, demonstrate it with a tiny code example, and give the reader a memorable mental model before moving on. That's exactly how difficult programming concepts should be taught: reduce the cognitive load without sacrificing the underlying principle.

The real beauty of Rust's ownership model is that it makes responsibility explicit: who owns the data, who can access it, and who is allowed to change it. Once those relationships become intuitive, a lot of Rust's seemingly complicated rules start to make sense.

And honestly, if more programming tutorials made learning feel this approachable, we'd probably have a lot more beginners willing to give Rust a chance. Keep the cats coming! πŸˆπŸ¦€

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

Thank you so much for such a thoughtful comment! πŸ˜ΈπŸ’š

The point about lifetimes and non-lexical lifetimes is especially helpful. I kept this post intentionally small, so that feels like a really nice next step for readers once the basic ownership model starts to click.

And I love this description

β€œless like an angry crab and more like a very strict colleague.” πŸ˜ΉπŸ¦€

That’s probably a much healthier way to think about the borrow checker.
Thanks again for adding so much useful context! 🐈✨

Collapse
 
programmer_arnab_96fb057e profile image
Programmer Arnab • • Edited

Worth adding: the move example only applies to non-Copy types.

For i32, let b = a; copies,
so a stays usable.

Also, with NLL, a &mut borrow ends

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

I added a small note to the article about Copy types. πŸ˜ΈπŸ’š

Nice contribution! Thanks for catching that and helping make the explanation more accurate. πŸ¦€βœ¨

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

the crab as the borrow checker is a good bit. one cat, one owner, and the compiler keeping count is the whole rule in one picture.

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

Haha, exactly! πŸ˜ΈπŸ¦€
One cat, one owner, and one very strict crab keeping track of everything.

Thank you for your kind word! πŸˆπŸ’š

Collapse
 
enoch_hou profile image
Enoch Hou •

η”¨ηŒ«ζ₯θ§£ι‡Šζ‰€ζœ‰ζƒθΏ™δΈͺζ―”ε–»ε€ͺε¦™δΊ†οΌŒθ½»ζΎεˆε₯½ζ‡‚οΌŒθ―»εΎ—εΎˆεΌ€εΏƒοΌ

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

Thank you! πŸ˜ΈπŸ’š

I’m really glad you enjoyed the analogy.

Rust ownership feels a little less scary when cats are involved. πŸˆπŸ¦€βœ¨

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ Brain Buffering •

Bro, this was such a fun read!πŸ˜€

Collapse
 
nyaomaru profile image
nyaomaru Brain Buffering •

YAY!! πŸ˜ΈπŸ’š

Cats and crabs make Rust a little less scary. πŸˆπŸ¦€βœ¨

Collapse
 
alexshev profile image
Alex Shev •

The cat metaphor makes ownership approachable without pretending the compiler is doing magic. One useful extension would be to show where the metaphor breaks: borrowing is not sharing the cat, and lifetimes are about proving a reference cannot outlive what it points to. Those edge cases are where beginners usually need the next mental model.