1 00:00:05,940 --> 00:00:12,150 A pointer is a general concept for a variable that contains an address and memory, which that address 2 00:00:12,150 --> 00:00:13,590 points to some data. 3 00:00:13,950 --> 00:00:19,440 We've already used pointers in rest and the most common pointer is a reference. 4 00:00:19,470 --> 00:00:23,520 Remember, a reference is with the ampersand sign. 5 00:00:25,110 --> 00:00:31,770 These pointers don't have any types of additional capabilities, but a pointer that does give us an 6 00:00:31,770 --> 00:00:33,210 additional capability. 7 00:00:33,390 --> 00:00:34,860 Are smart pointers. 8 00:00:34,860 --> 00:00:37,890 Smart pointers originated and C++. 9 00:00:37,890 --> 00:00:39,720 So it's not unique to rust. 10 00:00:39,810 --> 00:00:47,430 The common smart pointers in rust are box which allocates values on the heap reference counting RC, 11 00:00:47,430 --> 00:00:53,670 which is a reference counting type that allows multiple ownerships and ref. 12 00:00:53,700 --> 00:00:54,270 Ref. 13 00:00:54,270 --> 00:00:57,930 Mute which are accessed through ref cell. 14 00:00:58,600 --> 00:01:04,000 Which enforces the borrowing rules at runtime instead of compile time. 15 00:01:05,270 --> 00:01:09,350 The first one we will cover is using a box smart pointer. 16 00:01:09,860 --> 00:01:13,800 A box is the most straightforward smart pointer Rust offers. 17 00:01:13,820 --> 00:01:18,110 This allows us to allocate data on the heap rather than the stack. 18 00:01:18,140 --> 00:01:23,000 However, the pointer to the heap data is going to remain on the stack. 19 00:01:23,000 --> 00:01:26,420 So let's look at a quick example of using a box. 20 00:01:27,790 --> 00:01:34,750 We will create a tuple called T and we'll give it the values 12 and eggs. 21 00:01:35,830 --> 00:01:43,870 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 22 00:01:43,870 --> 00:01:48,220 it create a reference k a pointer to T. 23 00:01:49,620 --> 00:01:55,950 So now we can print out our new box pointer. 24 00:02:01,850 --> 00:02:05,360 And I need to use curly brackets. 25 00:02:06,840 --> 00:02:07,500 There we go. 26 00:02:07,650 --> 00:02:08,940 So now if we run this. 27 00:02:10,010 --> 00:02:16,250 We see that our B variable which is our box point printed out 12 and eggs. 28 00:02:18,010 --> 00:02:18,550 And now. 29 00:02:18,550 --> 00:02:22,060 So tuples are created on the stack. 30 00:02:23,600 --> 00:02:34,100 And then when we did, when we created a new box, it was created on the heap, but B was stored on 31 00:02:34,100 --> 00:02:42,020 the stack, and then B just pointed to the address and memory on the heap where this was stored. 32 00:02:44,250 --> 00:02:48,860 So pointers also come with a DX reference operator. 33 00:02:48,870 --> 00:02:53,850 So let's first start out with creating a simple reference. 34 00:02:59,350 --> 00:03:00,970 Or go X here. 35 00:03:01,240 --> 00:03:01,640 So. 36 00:03:02,320 --> 00:03:04,890 And X and x. 37 00:03:04,900 --> 00:03:05,470 All right. 38 00:03:05,740 --> 00:03:10,630 And now we will make sure that these are equal. 39 00:03:14,080 --> 00:03:15,340 And five. 40 00:03:16,220 --> 00:03:17,030 And why. 41 00:03:18,320 --> 00:03:25,010 So if we cargo run this, we actually see that we get an air and it's saying we cannot compare an integer 42 00:03:25,400 --> 00:03:28,130 with a reference to an integer. 43 00:03:29,060 --> 00:03:36,530 So what we need to do here is actually allocate our reference by using the asterisk and that the allocated 44 00:03:36,530 --> 00:03:40,970 forest and now when we run it, we see that we don't get that air anymore. 45 00:03:41,180 --> 00:03:47,390 So the allocation returns is going to give us the value at that memory address. 46 00:03:48,680 --> 00:03:52,490 And then likewise we can do this with a box. 47 00:03:52,820 --> 00:03:56,420 So we will say Let X equals five. 48 00:03:58,040 --> 00:04:02,360 Let Y equals box new. 49 00:04:03,300 --> 00:04:04,260 Of X. 50 00:04:07,120 --> 00:04:09,960 Then we'll do the same exact checks again. 51 00:04:10,320 --> 00:04:11,640 And if we run this. 52 00:04:12,840 --> 00:04:14,400 Everything runs successfully. 53 00:04:15,330 --> 00:04:25,530 And then just to verify, if we print out our box, we expect five to be returned to us. 54 00:04:25,530 --> 00:04:30,030 So let's print it out and just verify and we get five. 55 00:04:31,230 --> 00:04:34,900 And honestly, that is truly all there is to it for boxes. 56 00:04:34,920 --> 00:04:42,090 It is a very straightforward concept and it's just a way for us to allocate data onto the heat.