I hope you enjoyed this section on Smart Pointers and rust.
We are now going to take some time to go over to the solution to the assignment, starting with question
one, which is create a variable on the stack and also create a variable on the heap, multiply their
values and print out the result.
So we'll start out with creating a variable on the stack.
So we'll assign five, and now we need to create a variable on the heap.
So we'll call it Val two and to create a variable on the heap, we can simply just use a box smart pointer.
So we will create a new box which establishes it on the heap and we will assign it the value of two.
So now our two integer is going to be created on the heap.
And now we want to multiply their values and print out the result so we can use the print line macro
and we can say Val times val too.
So this in its current state will not execute correctly because we need to d reference value too.
And now if we run it, we see that we have the value ten.
So remember creating a value on the heap.
We can simply use a box smart pointer and then to get that value off the heap and a format that we can
utilize it, we need to d reference the value.
So now moving on to question two, we want to create a variable that holds a string.
So we will say let our C value equals string from our C value.
Now we want to create a reference counting smart pointer that points to the string above.
So the first thing we need to do is actually bring in reference counting smart pointer into our scope
so we can use the standard RC and then RC to bring it into scope for us.
And now we can say let RC equals excuse me of RC type string
equals RC
new RC value.
So what we're doing here is creating a reference counting smart pointer to r string variable RC value
above.
And now we want to print out how many references the smart pointer has so we can use our print line
macro and we can very easily get this value by using RC strong
count and then pass in a reference.
To our smart pointer.
So let's run that to make sure and we see that we have outputted the value of one which is currently
correct because the only reference we have right now to our C value is our RC smart pointer.
So now we're going to go down into this code block and we're going to create another reference counting
smart pointer.
So let's just go ahead and we will copy and paste this.
We'll call this RC two and we want it to point to our first smart pointer.
So our RC, so we will call this RC here and now we want to print out how many references each smart
pointer has.
So again, we will take this and we're good there and now we put two there.
So now if we run this, what is it?
What do we have?
We expected a string and found our C
and that's because I did knew right here we want to clone it.
So now if we run it, we have the correct results.
That's what copying and pasting will get you.
So now we see that we have two and two here and that is also correct.
So now, now what we have is we have both of these smart pointers pointing to the exact same value on
the heap.
Therefore the reference count to that value on the heap is two.
But now we exit out of this code block.
So what value is going to be dropped here?
Well, RC two is going to be dropped when we leave this scope because it was created in this scope.
So now if we print out how many references our first smart pointer has, we would expect the answer
the the value to be outputted to be one.
So if we run this again, we now see that we have one being printed out because we no longer have RC
two pointing to that value on the heat.
Therefore, our reference count is documented by one.
So again, we exit out of this scope of code.
So what value is going to be dropped here?
Well, in this code block is where we created our first smart pointer.
RC so RC is going to be what is dropped here.
So now that RC is dropped, what happens if we try to run this code where we try to reference RC value
and remember RC value is what we used here to create our first reference counted smart pointer.
So what do you think will happen when we try to run the program now?
Well, I expect we're going to get an error because we moved RC value into our RC pointer.
So let's run this and see what we get.
So we see a move occurs from RC value and it's because the move occurred here when we assigned our our
RC value string to our actual smart pointer and then we try to to, to use that value after it had already
been moved.
So basically what happened is, is once our first smart pointer, RC was dropped, RC value is also
dropped.
So hopefully that makes sense.
If you have any questions about this assignment, please feel free to ask them in the Q&A section.