1 00:00:05,600 --> 00:00:10,310 I hope you enjoyed this section on Smart Pointers and rust. 2 00:00:10,670 --> 00:00:15,800 We are now going to take some time to go over to the solution to the assignment, starting with question 3 00:00:15,800 --> 00:00:22,640 one, which is create a variable on the stack and also create a variable on the heap, multiply their 4 00:00:22,640 --> 00:00:24,590 values and print out the result. 5 00:00:24,950 --> 00:00:29,900 So we'll start out with creating a variable on the stack. 6 00:00:29,900 --> 00:00:35,480 So we'll assign five, and now we need to create a variable on the heap. 7 00:00:35,600 --> 00:00:42,630 So we'll call it Val two and to create a variable on the heap, we can simply just use a box smart pointer. 8 00:00:42,650 --> 00:00:50,090 So we will create a new box which establishes it on the heap and we will assign it the value of two. 9 00:00:50,210 --> 00:00:56,330 So now our two integer is going to be created on the heap. 10 00:00:57,310 --> 00:01:03,640 And now we want to multiply their values and print out the result so we can use the print line macro 11 00:01:05,860 --> 00:01:09,730 and we can say Val times val too. 12 00:01:10,510 --> 00:01:18,040 So this in its current state will not execute correctly because we need to d reference value too. 13 00:01:18,220 --> 00:01:22,420 And now if we run it, we see that we have the value ten. 14 00:01:22,420 --> 00:01:26,080 So remember creating a value on the heap. 15 00:01:26,080 --> 00:01:34,480 We can simply use a box smart pointer and then to get that value off the heap and a format that we can 16 00:01:34,480 --> 00:01:37,870 utilize it, we need to d reference the value. 17 00:01:38,920 --> 00:01:44,020 So now moving on to question two, we want to create a variable that holds a string. 18 00:01:44,950 --> 00:01:56,590 So we will say let our C value equals string from our C value. 19 00:01:59,680 --> 00:02:06,700 Now we want to create a reference counting smart pointer that points to the string above. 20 00:02:06,700 --> 00:02:14,380 So the first thing we need to do is actually bring in reference counting smart pointer into our scope 21 00:02:14,380 --> 00:02:22,090 so we can use the standard RC and then RC to bring it into scope for us. 22 00:02:22,090 --> 00:02:33,940 And now we can say let RC equals excuse me of RC type string 23 00:02:36,460 --> 00:02:38,410 equals RC 24 00:02:40,690 --> 00:02:45,070 new RC value. 25 00:02:45,070 --> 00:02:53,560 So what we're doing here is creating a reference counting smart pointer to r string variable RC value 26 00:02:53,560 --> 00:02:54,460 above. 27 00:02:55,390 --> 00:03:02,590 And now we want to print out how many references the smart pointer has so we can use our print line 28 00:03:02,590 --> 00:03:14,560 macro and we can very easily get this value by using RC strong 29 00:03:16,750 --> 00:03:21,220 count and then pass in a reference. 30 00:03:23,910 --> 00:03:25,380 To our smart pointer. 31 00:03:26,670 --> 00:03:32,670 So let's run that to make sure and we see that we have outputted the value of one which is currently 32 00:03:32,670 --> 00:03:42,990 correct because the only reference we have right now to our C value is our RC smart pointer. 33 00:03:45,060 --> 00:03:50,190 So now we're going to go down into this code block and we're going to create another reference counting 34 00:03:50,190 --> 00:03:50,940 smart pointer. 35 00:03:50,940 --> 00:03:55,830 So let's just go ahead and we will copy and paste this. 36 00:03:56,850 --> 00:04:06,210 We'll call this RC two and we want it to point to our first smart pointer. 37 00:04:06,210 --> 00:04:15,870 So our RC, so we will call this RC here and now we want to print out how many references each smart 38 00:04:15,870 --> 00:04:16,980 pointer has. 39 00:04:16,980 --> 00:04:23,790 So again, we will take this and we're good there and now we put two there. 40 00:04:23,790 --> 00:04:26,100 So now if we run this, what is it? 41 00:04:30,360 --> 00:04:31,350 What do we have? 42 00:04:31,350 --> 00:04:36,510 We expected a string and found our C 43 00:04:38,730 --> 00:04:43,110 and that's because I did knew right here we want to clone it. 44 00:04:43,800 --> 00:04:47,700 So now if we run it, we have the correct results. 45 00:04:48,810 --> 00:04:50,790 That's what copying and pasting will get you. 46 00:04:51,060 --> 00:04:56,490 So now we see that we have two and two here and that is also correct. 47 00:04:56,490 --> 00:05:03,720 So now, now what we have is we have both of these smart pointers pointing to the exact same value on 48 00:05:03,720 --> 00:05:04,380 the heap. 49 00:05:04,380 --> 00:05:10,110 Therefore the reference count to that value on the heap is two. 50 00:05:10,710 --> 00:05:13,230 But now we exit out of this code block. 51 00:05:13,230 --> 00:05:15,600 So what value is going to be dropped here? 52 00:05:15,600 --> 00:05:22,170 Well, RC two is going to be dropped when we leave this scope because it was created in this scope. 53 00:05:22,320 --> 00:05:28,920 So now if we print out how many references our first smart pointer has, we would expect the answer 54 00:05:29,130 --> 00:05:32,610 the the value to be outputted to be one. 55 00:05:32,910 --> 00:05:41,760 So if we run this again, we now see that we have one being printed out because we no longer have RC 56 00:05:41,760 --> 00:05:44,220 two pointing to that value on the heat. 57 00:05:44,220 --> 00:05:47,730 Therefore, our reference count is documented by one. 58 00:05:48,480 --> 00:05:54,330 So again, we exit out of this scope of code. 59 00:05:54,330 --> 00:05:56,700 So what value is going to be dropped here? 60 00:05:56,700 --> 00:06:01,860 Well, in this code block is where we created our first smart pointer. 61 00:06:01,860 --> 00:06:06,300 RC so RC is going to be what is dropped here. 62 00:06:06,450 --> 00:06:19,110 So now that RC is dropped, what happens if we try to run this code where we try to reference RC value 63 00:06:19,110 --> 00:06:26,820 and remember RC value is what we used here to create our first reference counted smart pointer. 64 00:06:27,180 --> 00:06:30,690 So what do you think will happen when we try to run the program now? 65 00:06:30,690 --> 00:06:39,120 Well, I expect we're going to get an error because we moved RC value into our RC pointer. 66 00:06:39,480 --> 00:06:41,730 So let's run this and see what we get. 67 00:06:42,030 --> 00:06:49,290 So we see a move occurs from RC value and it's because the move occurred here when we assigned our our 68 00:06:49,830 --> 00:06:59,400 RC value string to our actual smart pointer and then we try to to, to use that value after it had already 69 00:06:59,400 --> 00:07:00,180 been moved. 70 00:07:00,300 --> 00:07:09,270 So basically what happened is, is once our first smart pointer, RC was dropped, RC value is also 71 00:07:09,270 --> 00:07:10,020 dropped. 72 00:07:10,050 --> 00:07:11,790 So hopefully that makes sense. 73 00:07:11,970 --> 00:07:17,610 If you have any questions about this assignment, please feel free to ask them in the Q&A section.