A pointer is a general concept for a variable that contains an address and memory, which that address
points to some data.
We've already used pointers in rest and the most common pointer is a reference.
Remember, a reference is with the ampersand sign.
These pointers don't have any types of additional capabilities, but a pointer that does give us an
additional capability.
Are smart pointers.
Smart pointers originated and C++.
So it's not unique to rust.
The common smart pointers in rust are box which allocates values on the heap reference counting RC,
which is a reference counting type that allows multiple ownerships and ref.
Ref.
Mute which are accessed through ref cell.
Which enforces the borrowing rules at runtime instead of compile time.
The first one we will cover is using a box smart pointer.
A box is the most straightforward smart pointer Rust offers.
This allows us to allocate data on the heap rather than the stack.
However, the pointer to the heap data is going to remain on the stack.
So let's look at a quick example of using a box.
We will create a tuple called T and we'll give it the values 12 and eggs.
And then in here we'll create a box and we'll call it B and we'll say box new and then we'll just have
it create a reference k a pointer to T.
So now we can print out our new box pointer.
And I need to use curly brackets.
There we go.
So now if we run this.
We see that our B variable which is our box point printed out 12 and eggs.
And now.
So tuples are created on the stack.
And then when we did, when we created a new box, it was created on the heap, but B was stored on
the stack, and then B just pointed to the address and memory on the heap where this was stored.
So pointers also come with a DX reference operator.
So let's first start out with creating a simple reference.
Or go X here.
So.
And X and x.
All right.
And now we will make sure that these are equal.
And five.
And why.
So if we cargo run this, we actually see that we get an air and it's saying we cannot compare an integer
with a reference to an integer.
So what we need to do here is actually allocate our reference by using the asterisk and that the allocated
forest and now when we run it, we see that we don't get that air anymore.
So the allocation returns is going to give us the value at that memory address.
And then likewise we can do this with a box.
So we will say Let X equals five.
Let Y equals box new.
Of X.
Then we'll do the same exact checks again.
And if we run this.
Everything runs successfully.
And then just to verify, if we print out our box, we expect five to be returned to us.
So let's print it out and just verify and we get five.
And honestly, that is truly all there is to it for boxes.
It is a very straightforward concept and it's just a way for us to allocate data onto the heat.