So rust is known for it ownership, ownership rules (one owner, fixed place in memory) are not enough.
So Rust gives us special pointer types that wrap around values to add new powers:
Box → Put stuff on the heap
Rc → Share stuff (multiple owners)
Arc → Share stuff across threads
Pin → Prevent stuff from moving in memory
Box (Heap Allocation)
In Rust (and many programming languages), data can live in two main places: the stack and the heap. The stack works like a plate dispenser in a cafeteria, you can only put plates on top and take plates off the top. This makes the stack very fast, but also limited in size. Rust uses the stack for small, fixed-size values like numbers or simple structs.
The heap, on the other hand, is more like a big storage room. You can put things anywhere in the room and later retrieve them using a “ticket,” which is essentially a pointer. The heap is slower than the stack, but it offers much more space. Rust stores larger, flexible, or unknown-size data here, such as big collections, strings, or recursive types.Think of Box as a cardboard box. You put a value inside, and Rust puts it on the heap instead of the stack.
Rule: Only one owner of a Box. When the owner goes away, the box is destroyed.
Rc — Reference Counted (Single-thread)
Rc can be seen as one item that multiple people can share, but they all must stay in the same place (the same thread).
Rc
doesn’t allow multi-threading.
Think of Rc like a shared toy with a counter:
Every time a kid borrows the toy, the counter goes up.
When a kid stops using it, the counter goes down.
When the counter hits 0, the toy is thrown away.
Output:
Arc (Atomic Reference Counted)
This is very similar to Rc<T>
. The only difference is that it supports multi-threading.
Illustration: Imagine an item that multiple people in different locations want to use (that is why multi-threading).
Pin (No Moving Allowed)
Rust normally allows us to move values around in memory freely.
But sometimes moving breaks code, so Pin, as the name suggests, pins down a value in such a way that it can never be moved.
Pin<T>
says:
“Once this is placed in memory, it will never move again.”
You can still read and write the value.
But the memory address will never change.
Almighty Closure
I really spent time on this to understand, and it is very okay if you don’t understand it at once.
But I will try my best to explain it at the lowest level possible.
A closure is a little functional memory that helps you remember things.
A closure is like a tiny piece of memory with a job attached — it does some work for you and remembers what happened before, so it can continue from where it left off.
A Practical Example
Let’s say we have a farm business, and we start the day with 10 bags of fertilizer, we want to keep track of what is going on with our fertilizer so we create two closures (remove_fertilizer and add_fertilizer) so it helps keep track of when a fertilizer is added or remove because as time goes on as human being we might not be able to keep track of number sometimes
Every time we call remove_fertilizer (our closure), one bag is used (subtract 1).
Every time we call add_fertilizer (our closure), one bag is added (plus 1).
At the end of the day, the closures help us know exactly how many bags are left.
In simple terms:
Closures are like little helpers that keep track of things for you, so you don’t have to remember everything yourself.