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}");
π¦ Move::Wait... Where's My Cat?!
What happens if we try to use a again?
println!("{a}"); // β Compile error!
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-
Copytypes like String. Simple types such asi32implement Copy, solet 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}");
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}");
Output π
cat and another cat
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}"); // β
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");
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)
Thanks! This is where I finally grasped the concept of "ownership"! ππ¦
So, if I understand correctly: without
&, the "cat" moves tob, and trying to useaagain causes a compilation error. With&, however, the cat is merely "lent out," soaremains usable.In JS, writing
let b = awith 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! π
Exactly! πΈππ¦
Without
&, ownership of theStringmoves tob, soacanβt use it anymore. With&,bonly borrows it, andastays 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!
This makes me want to get a cat too π
Careful... thatβs how it starts. πΉπ
First you learn Rust ownership.
Then suddenly a cat owns you. π
lolπ
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.
That makes sense. Rust just wants us to take turns with the cat. ππ¦
Thanks for the extra detail! π
@nyaomaru arigatto for the first post for Brain Buffering, and congratulations- it blew up!πΊπ₯
And this...π
I wish cats could multiply like that; I'd have a thousandπ
This was a really creative one!β¨
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! π₯
haha, and the fact that the engagement is still growing! Sugoiβ¨πΊπ
Necoz is a whole mood ngl and I love itππ
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. πΉ
Hai! πΈπ©΅
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! ππ¦
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
Thatβs probably a much healthier way to think about the borrow checker.
Thanks again for adding so much useful context! πβ¨
Worth adding: the move example only applies to non-
Copytypes.For
i32,let b = a;copies,so
astays usable.Also, with NLL, a
&mutborrow endsI added a small note to the article about
Copytypes. πΈπNice contribution! Thanks for catching that and helping make the explanation more accurate. π¦β¨
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.
Haha, exactly! πΈπ¦
One cat, one owner, and one very strict crab keeping track of everything.
Thank you for your kind word! ππ
η¨η«ζ₯θ§£ιζζζθΏδΈͺζ―ε»ε€ͺε¦δΊοΌθ½»ζΎεε₯½ζοΌθ―»εΎεΎεΌεΏοΌ
Thank you! πΈπ
Iβm really glad you enjoyed the analogy.
Rust ownership feels a little less scary when cats are involved. ππ¦β¨
Bro, this was such a fun read!π
YAY!! πΈπ
Cats and crabs make Rust a little less scary. ππ¦β¨
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.