1 00:00:06,947 --> 00:00:07,780 - Okay. 2 00:00:07,780 --> 00:00:09,060 So, in my Main, 3 00:00:09,060 --> 00:00:12,570 I'm going to uncomment this statement, 4 00:00:12,570 --> 00:00:14,853 this is our statement for this example. 5 00:00:16,590 --> 00:00:19,860 And let's have a look at the code, demo box three. 6 00:00:19,860 --> 00:00:22,320 There's quite a lot of code in here. 7 00:00:22,320 --> 00:00:24,783 I'm gonna explain most of it. 8 00:00:25,620 --> 00:00:29,490 What I suggest you do is to have a look at all of it, 9 00:00:29,490 --> 00:00:33,160 when you get a chance, to digest in the kind of calmness 10 00:00:35,936 --> 00:00:36,960 of your everyday life. 11 00:00:36,960 --> 00:00:40,170 So you can actually get a chance to absorb what's going on. 12 00:00:40,170 --> 00:00:44,220 I've called it chain, but it's really like a link list. 13 00:00:44,220 --> 00:00:45,960 Okay, but I called it chain 14 00:00:45,960 --> 00:00:48,000 just to make it something different. 15 00:00:48,000 --> 00:00:51,090 So first of all, just to confirm the fact, 16 00:00:51,090 --> 00:00:53,280 the original approach that we tried, 17 00:00:53,280 --> 00:00:55,140 where we didn't use box, 18 00:00:55,140 --> 00:00:58,260 we just tried to kind of nest the item inside, 19 00:00:58,260 --> 00:01:00,990 that won't work, because it's infinitely nested, 20 00:01:00,990 --> 00:01:03,780 and therefore of infinite size. 21 00:01:03,780 --> 00:01:05,940 So if I uncomment that code, 22 00:01:05,940 --> 00:01:08,193 I will get an error from the compiler. 23 00:01:09,450 --> 00:01:10,950 I'm gonna show you the error. 24 00:01:10,950 --> 00:01:12,930 Recursive without indirection. 25 00:01:12,930 --> 00:01:15,000 It's a recursive data structure. 26 00:01:15,000 --> 00:01:17,670 A data structure that recurs to itself. 27 00:01:17,670 --> 00:01:19,560 But without using pointers. 28 00:01:19,560 --> 00:01:21,360 You can't just nest objects inside each other, 29 00:01:21,360 --> 00:01:22,800 it's like a hall of mirrors. 30 00:01:22,800 --> 00:01:25,260 There has to be some pointer to externalize it. 31 00:01:25,260 --> 00:01:28,170 So I get an error, compiler error. 32 00:01:28,170 --> 00:01:30,033 Cargo run. 33 00:01:33,360 --> 00:01:36,480 Okay, so it's complaining here. 34 00:01:36,480 --> 00:01:39,990 The node bad structure is a bad node, 35 00:01:39,990 --> 00:01:43,740 because it has a recursive data structure. 36 00:01:43,740 --> 00:01:45,600 Without using redirection. 37 00:01:45,600 --> 00:01:48,660 Consider using some kind of smart pointer. 38 00:01:48,660 --> 00:01:51,540 Okay, so we're talking about box at the moment. 39 00:01:51,540 --> 00:01:55,350 Later on in this lesson, we'll have a look at RC, 40 00:01:55,350 --> 00:01:58,470 which is a reference counted pointer. 41 00:01:58,470 --> 00:02:00,570 So, okay, so that doesn't work. 42 00:02:00,570 --> 00:02:03,420 So let's just comment that code back up again. 43 00:02:03,420 --> 00:02:06,270 I'm gonna just get rid of my terminal window for now, 44 00:02:06,270 --> 00:02:08,610 I'm not gonna run the application again for a few minutes. 45 00:02:08,610 --> 00:02:10,470 I just want to step through the code 46 00:02:10,470 --> 00:02:12,120 and explain what's going on. 47 00:02:12,120 --> 00:02:14,490 So if I give myself a bit more space. 48 00:02:14,490 --> 00:02:16,980 I want to bring up a whiteboard, 49 00:02:16,980 --> 00:02:19,683 so I can draw some pictures to explain the code. 50 00:02:20,880 --> 00:02:23,880 Okay, so I'm going to step through the code. 51 00:02:23,880 --> 00:02:25,560 And I'm gonna draw some pictures 52 00:02:25,560 --> 00:02:27,540 in my whiteboard on the right hand side, 53 00:02:27,540 --> 00:02:29,070 to see what's going on. 54 00:02:29,070 --> 00:02:29,903 Okay? 55 00:02:29,903 --> 00:02:33,030 So, in terms of the data for a node, 56 00:02:33,030 --> 00:02:37,530 a node, an individual node, will look like this. 57 00:02:37,530 --> 00:02:39,693 It'll have an integer data. 58 00:02:41,340 --> 00:02:42,483 Like 42. 59 00:02:43,350 --> 00:02:45,930 And then it'll have a next field, on line nine, 60 00:02:45,930 --> 00:02:47,430 and my code there on the left. 61 00:02:48,690 --> 00:02:50,640 And that next field is going to be an option. 62 00:02:50,640 --> 00:02:52,500 It's gonna get quite difficult to draw 63 00:02:52,500 --> 00:02:55,170 if I keep on showing an option that contains a box. 64 00:02:55,170 --> 00:02:56,310 So what I'll do, 65 00:02:56,310 --> 00:02:59,280 is I'll just imagine that there is an option there. 66 00:02:59,280 --> 00:03:01,560 But really it contains a box. 67 00:03:01,560 --> 00:03:04,440 Okay, so, it's not the last node in the list, 68 00:03:04,440 --> 00:03:05,273 in other words. 69 00:03:05,273 --> 00:03:07,770 So next is kinda like a box, really. 70 00:03:07,770 --> 00:03:10,713 Which contains a pointer to the next node. 71 00:03:12,390 --> 00:03:13,223 Okay? 72 00:03:13,223 --> 00:03:16,110 So that's the kind of data structure that we've got. 73 00:03:16,110 --> 00:03:20,223 Let's imagine that we've constructed a link list, or chain, 74 00:03:21,330 --> 00:03:26,100 with just two items, 43. 75 00:03:26,100 --> 00:03:28,980 So the second item, or the second node, 76 00:03:28,980 --> 00:03:31,290 it'll have an option. 77 00:03:31,290 --> 00:03:34,410 But that option, instead of containing some box, 78 00:03:34,410 --> 00:03:35,520 it contained none. 79 00:03:35,520 --> 00:03:36,930 So I just draw it as none, 80 00:03:36,930 --> 00:03:39,270 like that, it's like a null pointer. 81 00:03:39,270 --> 00:03:41,340 I've always drawn it when I'm teaching in other languages, 82 00:03:41,340 --> 00:03:43,530 I draw it as a null pointer, like that, 83 00:03:43,530 --> 00:03:45,980 that's effectively what we are trying to achieve. 84 00:03:46,980 --> 00:03:51,270 Okay, right, so, to when I wanna create a node, 85 00:03:51,270 --> 00:03:55,350 maybe to insert a new node into the chain, 86 00:03:55,350 --> 00:03:57,153 I've got a new function here. 87 00:03:58,170 --> 00:04:00,330 I just pass in the data that I wanna store, 88 00:04:00,330 --> 00:04:04,890 like maybe the value 44, I've got 42 and 43 already. 89 00:04:04,890 --> 00:04:09,240 So I pass in a data value that I'd like my node to contain, 90 00:04:09,240 --> 00:04:13,740 and I create a new node object which contains that data. 91 00:04:13,740 --> 00:04:16,320 But then who's next is none, okay, 92 00:04:16,320 --> 00:04:18,120 so the next value here is none. 93 00:04:18,120 --> 00:04:21,393 So whenever I create a new node, 94 00:04:22,320 --> 00:04:24,933 it'll contain the data that I pass in the parameter. 95 00:04:26,190 --> 00:04:28,737 Like the value 44, like that. 96 00:04:28,737 --> 00:04:33,737 And the next pointer, or the next box, will be none. 97 00:04:34,290 --> 00:04:37,680 Okay, because, well, because of the way this is gonna work, 98 00:04:37,680 --> 00:04:40,140 I'm gonna be appending at the end. 99 00:04:40,140 --> 00:04:44,613 So, making the next pointer effectively null makes sense. 100 00:04:45,810 --> 00:04:48,540 Alright, well, have us discuss that. 101 00:04:48,540 --> 00:04:50,060 Have a look at the append function there 102 00:04:50,060 --> 00:04:52,170 on the left hand side. 103 00:04:52,170 --> 00:04:57,170 The append function is going to, it's a recursive function. 104 00:04:58,230 --> 00:05:02,070 I'm going to arrange it so that you can only append 105 00:05:02,070 --> 00:05:04,680 at the end of the list, okay? 106 00:05:04,680 --> 00:05:07,230 So, it's kinda like a forward-only list, 107 00:05:07,230 --> 00:05:09,030 like you'd have in C++. 108 00:05:09,030 --> 00:05:12,870 It doesn't support insertion at random positions, 109 00:05:12,870 --> 00:05:15,300 and it doesn't support like a push front function, 110 00:05:15,300 --> 00:05:19,020 it only supports a push back mechanism. 111 00:05:19,020 --> 00:05:23,550 Okay, so, let's imagine that by one way or another, 112 00:05:23,550 --> 00:05:25,950 and I'll explain how this works in a moment. 113 00:05:25,950 --> 00:05:29,040 But imagine that this node on the left is the head node, 114 00:05:29,040 --> 00:05:30,900 and imagine that we kind of know 115 00:05:30,900 --> 00:05:32,850 that that's the head node, okay? 116 00:05:32,850 --> 00:05:34,860 What I'm gonna show you in a few minutes, 117 00:05:34,860 --> 00:05:37,500 I've actually got another structure called chain, 118 00:05:37,500 --> 00:05:40,380 which is really list in disguise. 119 00:05:40,380 --> 00:05:42,930 And it's got a box, a pointer, 120 00:05:42,930 --> 00:05:46,233 which points to the head node, like that, okay? 121 00:05:47,130 --> 00:05:50,100 But we'll see the details for that in a few minutes, 122 00:05:50,100 --> 00:05:52,590 let's ignore that part of it for now. 123 00:05:52,590 --> 00:05:55,140 And let's just imagine that one way or another 124 00:05:55,140 --> 00:05:59,100 we know that the box I've drawn here on the left hand side, 125 00:05:59,100 --> 00:06:01,140 we know that it's the head one. 126 00:06:01,140 --> 00:06:04,320 And what I do is I call the append function on here. 127 00:06:04,320 --> 00:06:07,140 I do something like, let's say I've called it head. 128 00:06:07,140 --> 00:06:09,573 Head.append. 129 00:06:11,760 --> 00:06:15,630 And I want to append a node with a data value 44. 130 00:06:15,630 --> 00:06:18,750 Alright, so that's how I'd call the append function. 131 00:06:18,750 --> 00:06:22,620 Head.append, assuming that head is some magical pointer box 132 00:06:22,620 --> 00:06:24,510 that points to this first node. 133 00:06:24,510 --> 00:06:27,030 So what we've gotta do is we've gotta basically recurse, 134 00:06:27,030 --> 00:06:30,930 we've gotta get to a point where we know we're at the end, 135 00:06:30,930 --> 00:06:34,050 and at that point, we can put the new node at this position. 136 00:06:34,050 --> 00:06:35,790 So the append function, 137 00:06:35,790 --> 00:06:37,980 the node is a recursive data structure, 138 00:06:37,980 --> 00:06:41,730 a node is a piece of data, and a pointer to the next node, 139 00:06:41,730 --> 00:06:45,120 box is pointer, really, a smart pointer. 140 00:06:45,120 --> 00:06:46,740 So when you have a recursive data structure, 141 00:06:46,740 --> 00:06:48,600 like a list or binary tree, 142 00:06:48,600 --> 00:06:51,240 the functions that you implement are typically recursive. 143 00:06:51,240 --> 00:06:54,240 They call themselves to step to the next node. 144 00:06:54,240 --> 00:06:55,950 And that's what I've done here. 145 00:06:55,950 --> 00:06:57,870 Have a look at the append function. 146 00:06:57,870 --> 00:06:59,760 So, wherever we're currently pointed to, 147 00:06:59,760 --> 00:07:03,393 so self will initially be pointing here, okay? 148 00:07:04,770 --> 00:07:07,203 It says, are you at the end of the chain? 149 00:07:08,160 --> 00:07:09,990 Let's have a look at your next pointer. 150 00:07:09,990 --> 00:07:13,500 I'm using the word pointer quite freely here, 151 00:07:13,500 --> 00:07:15,480 it's a box, really. 152 00:07:15,480 --> 00:07:17,100 But I'm using the word pointer, 153 00:07:17,100 --> 00:07:19,560 because that's what it is, really, it's a smart pointer. 154 00:07:19,560 --> 00:07:22,350 So, it's saying, look at the next pointer. 155 00:07:22,350 --> 00:07:23,700 Is it null? 156 00:07:23,700 --> 00:07:26,490 Option is effectively can be none, 157 00:07:26,490 --> 00:07:28,655 which is like a null pointer. 158 00:07:28,655 --> 00:07:33,000 And if it's none, we are currently at the end of the list. 159 00:07:33,000 --> 00:07:35,550 It's okay to append at the end of us. 160 00:07:35,550 --> 00:07:39,150 But, at the moment, our next reference isn't none, 161 00:07:39,150 --> 00:07:40,773 it's some box. 162 00:07:41,926 --> 00:07:44,673 Okay, so, this here is some. 163 00:07:45,690 --> 00:07:47,640 So what we do, is we get, 164 00:07:47,640 --> 00:07:49,320 there's some keywords here, ref mut, 165 00:07:49,320 --> 00:07:50,910 it's just a handy way of saying, 166 00:07:50,910 --> 00:07:54,093 give me back a mutable reference to. 167 00:07:55,320 --> 00:07:59,133 Right, so self.next is this option here. 168 00:08:00,780 --> 00:08:02,610 In the box, this option. 169 00:08:02,610 --> 00:08:03,870 And when you say some, 170 00:08:03,870 --> 00:08:07,020 it'll give you back the value inside the option. 171 00:08:07,020 --> 00:08:08,430 So I'm gonna get back, 172 00:08:08,430 --> 00:08:12,150 that will basically be a mutable reference 173 00:08:12,150 --> 00:08:14,043 to the box object. 174 00:08:15,690 --> 00:08:16,523 Like that. 175 00:08:16,523 --> 00:08:17,940 It's like the pointer. 176 00:08:17,940 --> 00:08:22,830 And then what I do is I say, that pointer basically points, 177 00:08:22,830 --> 00:08:25,533 that box points to this node here. 178 00:08:26,670 --> 00:08:29,760 So I say, dereference that box 179 00:08:29,760 --> 00:08:31,890 and call the append function on the thingy 0.2. 180 00:08:31,890 --> 00:08:33,780 So, I'm gonna draw this carefully now. 181 00:08:33,780 --> 00:08:35,793 This is a box object. 182 00:08:37,350 --> 00:08:39,243 And that box object has a pointer. 183 00:08:41,400 --> 00:08:44,790 This variable I've got here, this variable here. 184 00:08:44,790 --> 00:08:46,833 Basically is this box. 185 00:08:49,380 --> 00:08:50,250 All right. 186 00:08:50,250 --> 00:08:53,130 I wanna say object.append, remember, 187 00:08:53,130 --> 00:08:55,260 I was showing you this in the previous demo. 188 00:08:55,260 --> 00:08:57,480 When you have a box, and you call a function, 189 00:08:57,480 --> 00:09:00,750 what it really does is it is as if you did that. 190 00:09:00,750 --> 00:09:03,450 It will implicitly dereference the box, 191 00:09:03,450 --> 00:09:05,550 to give you the node that it points to. 192 00:09:05,550 --> 00:09:06,383 Okay? 193 00:09:06,383 --> 00:09:09,300 So when I say boxed_next_node.append 194 00:09:09,300 --> 00:09:12,660 really what I'm saying is, the pointer, inside here, 195 00:09:12,660 --> 00:09:14,820 points to this node. 196 00:09:14,820 --> 00:09:18,840 And it's upon this node that I call the append function. 197 00:09:18,840 --> 00:09:20,910 I've stepped down the list. 198 00:09:20,910 --> 00:09:22,860 And then it calls the append function recursively, 199 00:09:22,860 --> 00:09:23,733 on that node. 200 00:09:25,500 --> 00:09:27,000 Like that. 201 00:09:27,000 --> 00:09:28,154 Okay? 202 00:09:28,154 --> 00:09:30,900 So if you want to, you can dereference the box explicitly, 203 00:09:30,900 --> 00:09:34,500 if you prefer, you can dereference the box implicitly. 204 00:09:34,500 --> 00:09:35,790 That's weird, isn't it? 205 00:09:35,790 --> 00:09:38,190 A C++ developer would find this tricky. 206 00:09:38,190 --> 00:09:40,200 I did, coming from C++. 207 00:09:40,200 --> 00:09:41,040 So basically, what it does, 208 00:09:41,040 --> 00:09:43,050 it comes back up to the append function again, 209 00:09:43,050 --> 00:09:46,110 but this time we've marched on to the next node. 210 00:09:46,110 --> 00:09:48,210 We're trying to get to the last node, 211 00:09:48,210 --> 00:09:51,150 so that we can then append the item after us. 212 00:09:51,150 --> 00:09:52,320 So, let's have a look, 213 00:09:52,320 --> 00:09:53,820 we're currently pointing here. 214 00:09:54,930 --> 00:09:57,000 Let's have a look at the next field. 215 00:09:57,000 --> 00:10:00,450 Now the next field here is an option, and it's none. 216 00:10:00,450 --> 00:10:02,220 That's what I've been waiting for. 217 00:10:02,220 --> 00:10:04,710 It says, oh, if it's none, there's nothing after you, 218 00:10:04,710 --> 00:10:07,170 you've got a null next pointer, effectively. 219 00:10:07,170 --> 00:10:10,500 So in that case, great, in that case, we can create, 220 00:10:10,500 --> 00:10:11,850 now have a look at this code here, 221 00:10:11,850 --> 00:10:15,183 we're basically gonna create a new node and stick it here. 222 00:10:16,050 --> 00:10:17,280 Alright, that's what's gonna happen. 223 00:10:17,280 --> 00:10:20,160 I'm gonna move this picture a little bit to the left, 224 00:10:20,160 --> 00:10:21,760 to give myself a bit more space. 225 00:10:23,160 --> 00:10:24,180 Okay. 226 00:10:24,180 --> 00:10:27,300 And let's see what's going on in this piece of code here. 227 00:10:27,300 --> 00:10:30,483 So the most nested piece of the code is this bit here. 228 00:10:32,520 --> 00:10:36,360 So that will create a new node, containing the data, 229 00:10:36,360 --> 00:10:38,250 alright, so that's gonna create a new node, 230 00:10:38,250 --> 00:10:41,130 via the new function, the constructor, 231 00:10:41,130 --> 00:10:42,630 effectively, effectively. 232 00:10:42,630 --> 00:10:45,963 And it'll put the data, the value that we specify, just 44. 233 00:10:46,890 --> 00:10:49,770 And remember that when you create a new node, 234 00:10:49,770 --> 00:10:51,750 when you create a new node, 235 00:10:51,750 --> 00:10:55,800 it basically sets the new node to have a null next pointer. 236 00:10:55,800 --> 00:10:58,470 Or to put it in a Rust way, it has a box, 237 00:10:58,470 --> 00:11:01,680 it has an option, that's set to none. 238 00:11:01,680 --> 00:11:06,330 So, in a kind of a pointer-ish kind of dialect, 239 00:11:06,330 --> 00:11:10,200 I could say it has a null next pointer, like that. 240 00:11:10,200 --> 00:11:12,153 Okay, so it knows that it's at the end. 241 00:11:13,050 --> 00:11:15,870 Okay, so that's created the node object. 242 00:11:15,870 --> 00:11:20,700 And what I then do, is I put that pointer into a box, 243 00:11:20,700 --> 00:11:22,110 it would be allocated all in a heap. 244 00:11:22,110 --> 00:11:26,580 Okay, so that box object, we'll wrap it up. 245 00:11:26,580 --> 00:11:30,360 And that box object goes inside an option. 246 00:11:30,360 --> 00:11:33,390 And that option is what I store in my current next field. 247 00:11:33,390 --> 00:11:35,310 So this next field here, 248 00:11:35,310 --> 00:11:38,100 this is the next field that we currently at. 249 00:11:38,100 --> 00:11:41,883 That becomes an option. 250 00:11:43,710 --> 00:11:44,910 Okay, it is an option. 251 00:11:44,910 --> 00:11:48,150 And it contains some value as opposed to none. 252 00:11:48,150 --> 00:11:51,153 In particular, the option contains a box. 253 00:11:52,230 --> 00:11:55,323 So what I've just drawn in red is a box, this here. 254 00:11:56,443 --> 00:11:57,870 And I'll draw it again. 255 00:11:57,870 --> 00:11:59,970 That there is the box. 256 00:11:59,970 --> 00:12:02,490 And that box will contain a pointer 257 00:12:02,490 --> 00:12:05,160 to the node that's just been allocated, 258 00:12:05,160 --> 00:12:07,260 instead of being a null pointer, 259 00:12:07,260 --> 00:12:10,443 it now points here like that. 260 00:12:11,310 --> 00:12:12,497 Alright? 261 00:12:12,497 --> 00:12:14,010 So, to be perfectly honest, 262 00:12:14,010 --> 00:12:17,820 I mean I've been writing code like this in C++ for 30 years. 263 00:12:17,820 --> 00:12:20,100 And when I learned Rust, I found this hard. 264 00:12:20,100 --> 00:12:22,860 It really challenges your understanding, 265 00:12:22,860 --> 00:12:25,110 in C++ I could have written this in five minutes. 266 00:12:25,110 --> 00:12:26,820 With pointers, and I get that, 267 00:12:26,820 --> 00:12:28,350 and I've been doing it for years. 268 00:12:28,350 --> 00:12:32,760 With Rust, the idea of using this to represent the pointer, 269 00:12:32,760 --> 00:12:34,770 and then having to wrap it in an option, 270 00:12:34,770 --> 00:12:37,050 to represent the now pointer, 271 00:12:37,050 --> 00:12:39,600 is a very different mental model. 272 00:12:39,600 --> 00:12:41,130 So I had to really think this through 273 00:12:41,130 --> 00:12:43,230 when I was learning this myself. 274 00:12:43,230 --> 00:12:45,690 I'm gonna go through just a few orbits, 275 00:12:45,690 --> 00:12:46,523 and then I'll leave you 276 00:12:46,523 --> 00:12:48,780 have a look at full details yourself. 277 00:12:48,780 --> 00:12:52,050 What happens if you display a node? 278 00:12:52,050 --> 00:12:54,090 Okay, so if I just move my diagram 279 00:12:54,090 --> 00:12:55,623 back to the right hand side. 280 00:12:56,460 --> 00:12:59,040 And let's just see 281 00:12:59,040 --> 00:13:02,040 if I can get rid of some of my writing here. 282 00:13:02,040 --> 00:13:04,950 Having appended items into the list. 283 00:13:04,950 --> 00:13:07,620 So the list contains the data that we want. 284 00:13:07,620 --> 00:13:10,740 Let's say we want to display all the data. 285 00:13:10,740 --> 00:13:13,800 So the way you do that, is you call the display function, 286 00:13:13,800 --> 00:13:15,123 on the head node. 287 00:13:16,297 --> 00:13:20,100 And it ensures that it is printed, and so is the next one, 288 00:13:20,100 --> 00:13:23,220 and so is the next one in a recursive kinda way. 289 00:13:23,220 --> 00:13:25,650 So I'm gonna display function here. 290 00:13:25,650 --> 00:13:27,570 And what you would do is you would, again, 291 00:13:27,570 --> 00:13:29,430 you'd have some kind of head pointer, 292 00:13:29,430 --> 00:13:31,950 via the chain class. 293 00:13:31,950 --> 00:13:33,120 Structure, excuse me. 294 00:13:33,120 --> 00:13:37,560 And it would tell me where the head of my chain is. 295 00:13:37,560 --> 00:13:41,100 So let's say I've said head.display. 296 00:13:41,100 --> 00:13:43,800 So that would basically try to display the first node. 297 00:13:44,880 --> 00:13:46,200 Okay, well that's fair enough. 298 00:13:46,200 --> 00:13:47,370 In the display function, 299 00:13:47,370 --> 00:13:50,400 it prints the data for the first node. 300 00:13:50,400 --> 00:13:52,233 And that's going to be the value 42. 301 00:13:53,340 --> 00:13:54,173 Beautiful. 302 00:13:56,130 --> 00:13:56,963 Like that. 303 00:13:56,963 --> 00:13:57,810 And then it says, okay, 304 00:13:57,810 --> 00:14:00,090 but then I've gotta print the rest of the list. 305 00:14:00,090 --> 00:14:02,913 So, it says, am I at the end? 306 00:14:03,900 --> 00:14:05,643 Is the next pointer null? 307 00:14:06,840 --> 00:14:09,540 No, it isn't, it's some value. 308 00:14:09,540 --> 00:14:14,010 So, it'll basically get the box out of here. 309 00:14:14,010 --> 00:14:16,950 That's the box that the option contains. 310 00:14:16,950 --> 00:14:19,923 That box contains is here. 311 00:14:20,970 --> 00:14:21,803 Okay? 312 00:14:21,803 --> 00:14:26,580 And that box, again, I could have said here, tick the box. 313 00:14:26,580 --> 00:14:28,980 The box is effectively a pointer. 314 00:14:28,980 --> 00:14:30,453 Dereference the pointer. 315 00:14:32,826 --> 00:14:34,980 To get the actual node that you're talking to. 316 00:14:34,980 --> 00:14:37,590 So it would dereference this box, 317 00:14:37,590 --> 00:14:40,443 and gimme back the node like that. 318 00:14:41,700 --> 00:14:44,970 Or I can do that implicitly without the star. 319 00:14:44,970 --> 00:14:48,000 And believe it or not, people actually write it that way. 320 00:14:48,000 --> 00:14:49,620 Even C++ developers. 321 00:14:49,620 --> 00:14:51,270 So what you're basically saying is, 322 00:14:51,270 --> 00:14:54,030 you are moving to the next node. 323 00:14:54,030 --> 00:14:56,193 And then you call in display on that. 324 00:14:58,350 --> 00:14:59,183 Okay? 325 00:14:59,183 --> 00:15:00,750 And that'll go through the same process, 326 00:15:00,750 --> 00:15:02,100 it'll print the value, 327 00:15:02,100 --> 00:15:04,713 the data for the cabinet node, which is 43. 328 00:15:08,370 --> 00:15:11,580 And it'll then chain on to the next node, 329 00:15:11,580 --> 00:15:13,443 and the next node is here. 330 00:15:14,970 --> 00:15:15,803 Okay? 331 00:15:15,803 --> 00:15:18,630 And that'll print the data at the current node. 332 00:15:18,630 --> 00:15:20,823 So that'll be 44. 333 00:15:22,920 --> 00:15:26,400 And then it says, and by the way, what's your next pointer? 334 00:15:26,400 --> 00:15:27,300 Is it null? 335 00:15:27,300 --> 00:15:28,590 Yes, it is. 336 00:15:28,590 --> 00:15:31,110 None, really, in an option kinda way. 337 00:15:31,110 --> 00:15:32,910 So that means we've reached the end. 338 00:15:32,910 --> 00:15:36,510 So what I do is I just print end, just to confirm, 339 00:15:36,510 --> 00:15:38,040 just to get confirmation of the fact 340 00:15:38,040 --> 00:15:42,420 that it has actually reached the end, like that. 341 00:15:42,420 --> 00:15:43,253 There we are. 342 00:15:43,253 --> 00:15:45,630 So, it does take some getting used to, 343 00:15:45,630 --> 00:15:47,550 but you will get used to it. 344 00:15:47,550 --> 00:15:50,430 And it is a beautiful thing, really. 345 00:15:50,430 --> 00:15:54,810 It's much safer than the corresponding code in C++. 346 00:15:54,810 --> 00:15:57,330 I can't possibly dereference a null pointer here. 347 00:15:57,330 --> 00:15:58,860 I can't forget to deallocate anything, 348 00:15:58,860 --> 00:16:01,590 it's all managed for me, much safer. 349 00:16:01,590 --> 00:16:04,860 So what I'll do is just to finish off, 350 00:16:04,860 --> 00:16:09,000 I'll just delete my picture that I've got so far. 351 00:16:09,000 --> 00:16:10,410 Because there's another structure 352 00:16:10,410 --> 00:16:13,920 that I'll just introduced briefly, and that's chain. 353 00:16:13,920 --> 00:16:16,740 Chain is really the list class, if you like, 354 00:16:16,740 --> 00:16:17,970 in other programming languages. 355 00:16:17,970 --> 00:16:20,850 And it just contains a head pointer, if you like. 356 00:16:20,850 --> 00:16:23,310 I say pointer, I really mean box. 357 00:16:23,310 --> 00:16:27,693 It contains a pointer to the head node, or possibly null. 358 00:16:28,863 --> 00:16:31,860 Okay, so this, for a C++ developer, 359 00:16:31,860 --> 00:16:35,117 would be a pointer to a node that might be a null pointer, 360 00:16:35,117 --> 00:16:38,460 it's a nullable pointer to node. 361 00:16:38,460 --> 00:16:41,970 A Rust developer would say it's a boxed node. 362 00:16:41,970 --> 00:16:44,160 So the node will be allocated on the heap, 363 00:16:44,160 --> 00:16:45,760 and the box will have a pointer. 364 00:16:46,710 --> 00:16:48,213 Or it could be none. 365 00:16:49,680 --> 00:16:53,340 So when I create a new chain, in my code down below, 366 00:16:53,340 --> 00:16:56,370 in fact, let me just show you the code down below. 367 00:16:56,370 --> 00:16:58,263 When I create a new chain here. 368 00:17:01,500 --> 00:17:04,620 The constructor, effectively, it creates a chain, 369 00:17:04,620 --> 00:17:07,620 and just sets the head to be none. 370 00:17:07,620 --> 00:17:08,850 Head is an option. 371 00:17:08,850 --> 00:17:11,490 It can be some value, or it can be none. 372 00:17:11,490 --> 00:17:14,250 So, when you create a new chain, 373 00:17:14,250 --> 00:17:17,970 and it has a head, a box. 374 00:17:17,970 --> 00:17:20,040 Which is effectively a null pointer, 375 00:17:20,040 --> 00:17:23,790 to say it's an empty collection, at the moment. 376 00:17:23,790 --> 00:17:25,860 When you call the insert function, 377 00:17:25,860 --> 00:17:28,320 I've called the insert function four times, 378 00:17:28,320 --> 00:17:29,910 it's an append function, really, 379 00:17:29,910 --> 00:17:32,730 perhaps I should have called it append, but there we go. 380 00:17:32,730 --> 00:17:34,980 When you call the insert function, 381 00:17:34,980 --> 00:17:38,650 the insert function, basically, it has to decide 382 00:17:41,341 --> 00:17:45,810 whether to create a new structure, or basically, 383 00:17:45,810 --> 00:17:47,430 it needs to decide what to do about the head point, 384 00:17:47,430 --> 00:17:48,480 that's the key point. 385 00:17:49,650 --> 00:17:51,960 In the special case, it's only the first time, 386 00:17:51,960 --> 00:17:54,420 where the head pointer is effectively gonna be null. 387 00:17:54,420 --> 00:17:57,540 So, first time through, when we call the insert function, 388 00:17:57,540 --> 00:17:59,943 it'll say, is the head pointer null? 389 00:18:01,096 --> 00:18:02,700 And if it is, and it is, 390 00:18:02,700 --> 00:18:05,973 then it'll basically create a new node. 391 00:18:06,960 --> 00:18:08,103 It'll create a node. 392 00:18:09,450 --> 00:18:13,890 Put it into a box, put it into an option. 393 00:18:13,890 --> 00:18:15,900 And that's what our head variable is. 394 00:18:15,900 --> 00:18:20,900 So our head variable now, is an option that contains a box. 395 00:18:23,430 --> 00:18:26,250 And that box contains a pointer, 396 00:18:26,250 --> 00:18:29,283 to a new node that's being created like that. 397 00:18:30,420 --> 00:18:32,730 Alright, if we call the insert function again, 398 00:18:32,730 --> 00:18:35,127 it would say, is the head pointer null? 399 00:18:35,127 --> 00:18:36,360 And no it isn't. 400 00:18:36,360 --> 00:18:38,885 The head pointer points to a node. 401 00:18:38,885 --> 00:18:40,830 In that case, what it'll do 402 00:18:40,830 --> 00:18:43,830 is it'll basically tell that node to append. 403 00:18:43,830 --> 00:18:44,880 Okay, that's the function 404 00:18:44,880 --> 00:18:47,280 that I introduced about five minutes ago. 405 00:18:47,280 --> 00:18:50,310 When you have a pointer to the head node, 406 00:18:50,310 --> 00:18:54,810 and you call append, as we saw in the last five minutes, 407 00:18:54,810 --> 00:18:57,210 the append function basically figures out, 408 00:18:57,210 --> 00:18:59,280 there may be multiple nodes here. 409 00:18:59,280 --> 00:19:01,860 The append function will kind of recursively figure out 410 00:19:01,860 --> 00:19:02,850 where the last node is, 411 00:19:02,850 --> 00:19:06,720 and then it'll basically stick the new data at the end. 412 00:19:06,720 --> 00:19:08,460 And it's the same thing for display. 413 00:19:08,460 --> 00:19:12,540 When I display a chain, if my chain is empty, 414 00:19:12,540 --> 00:19:14,250 it just says empty chain. 415 00:19:14,250 --> 00:19:16,620 If the head chain isn't empty, then all it does 416 00:19:16,620 --> 00:19:19,980 is to say to the first node in my collection, 417 00:19:19,980 --> 00:19:21,303 basically the head node. 418 00:19:22,350 --> 00:19:24,420 It'll say basically display the head node. 419 00:19:24,420 --> 00:19:27,900 It'll call the display function on the first node. 420 00:19:27,900 --> 00:19:30,990 And again, from what we saw earlier, 421 00:19:30,990 --> 00:19:34,530 when you call display on a node, it displays itself, 422 00:19:34,530 --> 00:19:36,210 and then goes to the next one, 423 00:19:36,210 --> 00:19:38,010 and it keeps on doing that recursively, 424 00:19:38,010 --> 00:19:39,780 until it reaches the end. 425 00:19:39,780 --> 00:19:41,610 So I guess all I need to do now, 426 00:19:41,610 --> 00:19:43,170 is to actually run the application, 427 00:19:43,170 --> 00:19:46,320 and I rather hope it works after all that effort. 428 00:19:46,320 --> 00:19:49,950 So, create an empty chain, insert some items, append, 429 00:19:49,950 --> 00:19:52,560 and then display the whole chain at the end. 430 00:19:52,560 --> 00:19:54,693 I have all digits crossed. 431 00:19:55,650 --> 00:19:57,270 Cargo run. 432 00:19:57,270 --> 00:20:00,120 So I'm hoping it's going to, basically, 433 00:20:00,120 --> 00:20:03,810 it'll insert 100, and then append 200, and append 300. 434 00:20:03,810 --> 00:20:04,800 I want to display it. 435 00:20:04,800 --> 00:20:09,496 It'll display 100, 200, 300, and then end. 436 00:20:09,496 --> 00:20:10,393 (sighs)