1 00:00:06,630 --> 00:00:08,520 - Inheritance is a very important feature 2 00:00:08,520 --> 00:00:10,710 in object-oriented program languages. 3 00:00:10,710 --> 00:00:12,510 It's where you define a supertype 4 00:00:12,510 --> 00:00:15,570 that specifies some common behavior and data 5 00:00:15,570 --> 00:00:18,510 and then you define subtypes which inherit that data 6 00:00:18,510 --> 00:00:21,360 and functionality and can extend it. 7 00:00:21,360 --> 00:00:22,710 So that's how you might be familiar 8 00:00:22,710 --> 00:00:25,110 with object orientation in other languages. 9 00:00:25,110 --> 00:00:27,060 But in Rust it's quite different. 10 00:00:27,060 --> 00:00:30,270 Rust doesn't allow structures to inherit from each other. 11 00:00:30,270 --> 00:00:34,173 Instead, in Rust, inheritance is based on traits. 12 00:00:35,400 --> 00:00:37,680 Okay, so you have to implement a trait 13 00:00:37,680 --> 00:00:40,230 rather than inheriting from a structure. 14 00:00:40,230 --> 00:00:42,210 It's all based around traits. 15 00:00:42,210 --> 00:00:44,280 So here's a simple trait 16 00:00:44,280 --> 00:00:46,800 which specifies overridable behavior. 17 00:00:46,800 --> 00:00:48,720 It's a simple print trait. 18 00:00:48,720 --> 00:00:51,900 It can be implemented by any structures that want to. 19 00:00:51,900 --> 00:00:55,200 So here's a point structure 20 00:00:55,200 --> 00:00:59,040 and here's the implementation of the print trait for point. 21 00:00:59,040 --> 00:01:02,790 The point structure implements the print trait. 22 00:01:02,790 --> 00:01:05,820 The employee structure also implements the print trait. 23 00:01:05,820 --> 00:01:07,500 They both have a print function. 24 00:01:07,500 --> 00:01:09,720 They both implement the print function 25 00:01:09,720 --> 00:01:11,403 from the trait defined above. 26 00:01:12,840 --> 00:01:17,387 Okay, so Rust supports the Liskov Substitution Principle. 27 00:01:17,387 --> 00:01:21,810 So what this means is you declare a trait reference variable 28 00:01:21,810 --> 00:01:23,490 and then it can refer 29 00:01:23,490 --> 00:01:25,980 to any object which implements the trait. 30 00:01:25,980 --> 00:01:28,800 It's the principle of substitutability. 31 00:01:28,800 --> 00:01:30,270 Here's an example. 32 00:01:30,270 --> 00:01:32,490 I defined an object reference. 33 00:01:32,490 --> 00:01:36,270 So I've created a point object 34 00:01:36,270 --> 00:01:38,730 and I've created an employee object 35 00:01:38,730 --> 00:01:42,600 and then I can pass the point object. 36 00:01:42,600 --> 00:01:47,550 That's object one or the employee object, that's object two. 37 00:01:47,550 --> 00:01:50,850 I can pass references of those into this function 38 00:01:50,850 --> 00:01:53,880 because this function is effectively polymorphic. 39 00:01:53,880 --> 00:01:55,890 It receives a reference 40 00:01:55,890 --> 00:01:58,920 to anything which implements the print trait. 41 00:01:58,920 --> 00:02:02,250 So you can pass into this function any type 42 00:02:02,250 --> 00:02:05,703 which inherits one or which implements the print interface. 43 00:02:06,780 --> 00:02:11,280 Object one is a point object that implements print. 44 00:02:11,280 --> 00:02:14,400 I can pass a reference to object one into here. 45 00:02:14,400 --> 00:02:15,660 Object two. 46 00:02:15,660 --> 00:02:17,520 Object two is an employee. 47 00:02:17,520 --> 00:02:19,770 Employee also implements print. 48 00:02:19,770 --> 00:02:23,070 I can pass a reference to employee into here. 49 00:02:23,070 --> 00:02:27,900 You'll notice the dyn keyword or the dyn keyword. 50 00:02:27,900 --> 00:02:29,790 That's short for dynamic. 51 00:02:29,790 --> 00:02:34,500 The dyn keyword represents dynamic dispatch. 52 00:02:34,500 --> 00:02:39,240 Okay, it's basically similar to a virtual function in C++. 53 00:02:39,240 --> 00:02:41,640 It triggers dynamic dispatch. 54 00:02:41,640 --> 00:02:44,700 What it means is that when the compiler sees 55 00:02:44,700 --> 00:02:46,110 that you're calling the function 56 00:02:46,110 --> 00:02:48,630 through a dynamically dispatch variable, 57 00:02:48,630 --> 00:02:51,780 the compiler can't decide which function to call 58 00:02:51,780 --> 00:02:54,540 because at compile time it doesn't know what kind 59 00:02:54,540 --> 00:02:56,553 of object it'd be referring to. 60 00:02:57,540 --> 00:02:59,040 It could be referring to any object 61 00:02:59,040 --> 00:03:01,500 which implements the print trait. 62 00:03:01,500 --> 00:03:04,350 So the compiler can't decide what would be 63 00:03:04,350 --> 00:03:06,000 the appropriate print function call. 64 00:03:06,000 --> 00:03:09,510 It leaves that decision until runtime and at runtime 65 00:03:09,510 --> 00:03:12,180 when it knows what kind of objects actually being passed in 66 00:03:12,180 --> 00:03:15,330 then it can make a decision at runtime dynamically 67 00:03:15,330 --> 00:03:17,460 to which function gets called. 68 00:03:17,460 --> 00:03:21,510 So let's reconsider the example where we had an object. 69 00:03:21,510 --> 00:03:24,510 One was a point, object two was an employee. 70 00:03:24,510 --> 00:03:25,860 I could pass either a reference 71 00:03:25,860 --> 00:03:28,470 to a point or a reference to an employee 72 00:03:28,470 --> 00:03:31,350 or in fact a reference to anything which implements print. 73 00:03:31,350 --> 00:03:34,800 I can pass anything like that into this function here. 74 00:03:34,800 --> 00:03:36,450 P could be reference 75 00:03:36,450 --> 00:03:40,260 to anything which implements the print trait. 76 00:03:40,260 --> 00:03:42,060 Okay, so when you pass a trait in like this 77 00:03:42,060 --> 00:03:44,850 you have to qualify it with the dyn keyword. 78 00:03:44,850 --> 00:03:46,560 It's almost like reinstating 79 00:03:46,560 --> 00:03:48,840 into the compiler that you understand 80 00:03:48,840 --> 00:03:51,300 that it's going to be a dynamic dispatch. 81 00:03:51,300 --> 00:03:53,520 Okay, if you omitted the dyn keyword here, 82 00:03:53,520 --> 00:03:56,670 it wouldn't work actually, you get a compiler error. 83 00:03:56,670 --> 00:03:59,010 At runtime, so at compiler time, 84 00:03:59,010 --> 00:04:01,380 when the compiler sees the print function 85 00:04:01,380 --> 00:04:03,090 it doesn't know what kind of object 86 00:04:03,090 --> 00:04:05,070 at compile time has been passed in. 87 00:04:05,070 --> 00:04:08,490 Could have been a point, could have been a employee. 88 00:04:08,490 --> 00:04:11,040 So at compile time it doesn't make the decision 89 00:04:11,040 --> 00:04:14,040 about which function call, but at runtime it looks 90 00:04:14,040 --> 00:04:15,510 at the object you've passed in 91 00:04:15,510 --> 00:04:17,190 and that influences the decision 92 00:04:17,190 --> 00:04:18,390 which function will be called. 93 00:04:18,390 --> 00:04:22,230 So this statement here will actually have different effects. 94 00:04:22,230 --> 00:04:24,330 The first time I call print something 95 00:04:24,330 --> 00:04:28,470 with a point, at runtime, this will be a point object 96 00:04:28,470 --> 00:04:31,080 and it's the point print that would be called. 97 00:04:31,080 --> 00:04:34,410 In the second call I pass in an employee reference. 98 00:04:34,410 --> 00:04:37,110 So at runtime P will refer to an employee 99 00:04:37,110 --> 00:04:39,540 and it knows that it refers to an employee. 100 00:04:39,540 --> 00:04:42,660 So at runtime it'll call the employee statement. 101 00:04:42,660 --> 00:04:45,570 So this is effectively, this is polymorphism 102 00:04:45,570 --> 00:04:48,120 it's effectively like an if statement at runtime. 103 00:04:48,120 --> 00:04:52,320 If the object is a point, then call the point function. 104 00:04:52,320 --> 00:04:55,530 If it's an employee, then call the employee version. 105 00:04:55,530 --> 00:04:59,100 Internally, the way that dynamic dispatch works 106 00:04:59,100 --> 00:05:02,040 and trait interfaces implementation, 107 00:05:02,040 --> 00:05:03,960 it uses a V table mechanism 108 00:05:03,960 --> 00:05:06,120 similar to the way it works in C++. 109 00:05:06,120 --> 00:05:08,760 Basically a bunch of function point is to say 110 00:05:08,760 --> 00:05:11,340 if it's this kind of object call these functions 111 00:05:11,340 --> 00:05:12,720 if it's a different kind of object, 112 00:05:12,720 --> 00:05:14,253 call these functions instead. 113 00:05:15,300 --> 00:05:17,610 Right, well, so we are seeing an example 114 00:05:17,610 --> 00:05:20,430 of how to create a polymorphic collection here. 115 00:05:20,430 --> 00:05:23,430 You can create a collection based on a trait type. 116 00:05:23,430 --> 00:05:25,260 The collection can hold any objects 117 00:05:25,260 --> 00:05:26,910 which implement the trait 118 00:05:26,910 --> 00:05:29,940 and then methods would be called on each instance 119 00:05:29,940 --> 00:05:32,730 depending on what particular type that instance happens 120 00:05:32,730 --> 00:05:34,980 to be dynamically at runtime. 121 00:05:34,980 --> 00:05:39,900 So here vector is a vector of any object 122 00:05:39,900 --> 00:05:43,110 which can implement the print trait and again, 123 00:05:43,110 --> 00:05:46,320 you have to use the dyn keyword here almost to reinforce 124 00:05:46,320 --> 00:05:49,770 to the compiler that you do understand any method calls 125 00:05:49,770 --> 00:05:51,780 will be dynamically dispatched. 126 00:05:51,780 --> 00:05:55,290 I've got a vector which includes a point object, wasn't it? 127 00:05:55,290 --> 00:05:58,920 Object one was a point and object two was an employee. 128 00:05:58,920 --> 00:05:59,910 So when I iterate 129 00:05:59,910 --> 00:06:02,850 through the vector first time around the loop 130 00:06:02,850 --> 00:06:07,500 the object would be a point and dynamically at runtime 131 00:06:07,500 --> 00:06:11,010 the compiler will decide to call the point version of print. 132 00:06:11,010 --> 00:06:12,510 Second time around the loop, 133 00:06:12,510 --> 00:06:14,373 the object would be the employee. 134 00:06:15,660 --> 00:06:17,790 So dynamically at one time 135 00:06:17,790 --> 00:06:20,340 this will call the employee version of print. 136 00:06:20,340 --> 00:06:23,160 Okay, so this is a polymorphic collection. 137 00:06:23,160 --> 00:06:25,770 Right, so, in less than 12 traits 138 00:06:25,770 --> 00:06:28,170 these are the files we're going to look at in the demo. 139 00:06:28,170 --> 00:06:31,560 First of all, main.rs obviously to bootstrap the demo 140 00:06:31,560 --> 00:06:35,160 and then the traits we're gonna look in print.rs. 141 00:06:35,160 --> 00:06:38,040 And then in terms of implementation classes or structures 142 00:06:38,040 --> 00:06:40,020 we'll have a look at the point structure 143 00:06:40,020 --> 00:06:43,230 and the employee structure, both of which implement print. 144 00:06:43,230 --> 00:06:44,760 And then we'll use all that 145 00:06:44,760 --> 00:06:48,903 in our kind of driver code demo inheritance polymorphism. 146 00:06:49,890 --> 00:06:51,633 Okay, so let's take a look. 147 00:06:52,710 --> 00:06:54,150 Okay, right, well here we are then. 148 00:06:54,150 --> 00:06:57,150 So in main let me uncomp the code 149 00:06:57,150 --> 00:06:59,973 for inheritance and polymorphism. 150 00:07:01,170 --> 00:07:04,980 And then in the traits we have the print trait. 151 00:07:04,980 --> 00:07:08,940 Simple enough, it has a simple print function. 152 00:07:08,940 --> 00:07:11,010 And then in terms of structures 153 00:07:11,010 --> 00:07:16,010 well the employee structure implements print. 154 00:07:16,830 --> 00:07:18,600 Okay it implements other things as well. 155 00:07:18,600 --> 00:07:21,510 But that's all we're interested in at the moment. 156 00:07:21,510 --> 00:07:23,950 So the employee structure implements print 157 00:07:25,080 --> 00:07:27,330 and then the point structure, 158 00:07:27,330 --> 00:07:29,280 well here's the point structure. 159 00:07:29,280 --> 00:07:30,750 We can look at all its details 160 00:07:30,750 --> 00:07:32,370 but really all we need to know about 161 00:07:32,370 --> 00:07:34,920 is that it also implements print. 162 00:07:34,920 --> 00:07:37,080 So hopefully now you're getting to like the way 163 00:07:37,080 --> 00:07:40,410 that interfaces or traits are kind of sliced 164 00:07:40,410 --> 00:07:42,180 as individual implementation blocks. 165 00:07:42,180 --> 00:07:44,880 You can quite quickly hone in 166 00:07:44,880 --> 00:07:49,143 on the particular trait implementation for that structure. 167 00:07:51,090 --> 00:07:53,040 Right, okay, so in terms 168 00:07:53,040 --> 00:07:57,000 of driving the demo, demo inheritance polymorphism 169 00:07:57,000 --> 00:07:58,470 this is the code we're going to run. 170 00:07:58,470 --> 00:08:02,430 So we import the trait footprint. 171 00:08:02,430 --> 00:08:06,150 We import the structures for point and employee. 172 00:08:06,150 --> 00:08:11,130 I create a point object and an employee object. 173 00:08:11,130 --> 00:08:14,640 I pass a reference to the point and a reference 174 00:08:14,640 --> 00:08:17,340 to the employee into this function down here. 175 00:08:17,340 --> 00:08:19,500 And this function can receive anything 176 00:08:19,500 --> 00:08:21,630 which implements the print trait. 177 00:08:21,630 --> 00:08:25,110 So this is Liskov Substitution Principle. 178 00:08:25,110 --> 00:08:26,910 You can substitute in here anything 179 00:08:26,910 --> 00:08:28,620 which implements the print rate. 180 00:08:28,620 --> 00:08:30,960 And don't forget the dyn keyword 181 00:08:30,960 --> 00:08:33,330 to indicate dynamic dispatch. 182 00:08:33,330 --> 00:08:36,840 So the first time we call the print something function 183 00:08:36,840 --> 00:08:37,920 we pass in the point, 184 00:08:37,920 --> 00:08:41,280 at one time it'll be a point object that gets printed 185 00:08:41,280 --> 00:08:44,280 and it'll call the point print function correctly. 186 00:08:44,280 --> 00:08:46,170 And then the second time we call print something 187 00:08:46,170 --> 00:08:48,300 it'll be the employee object that gets passed in 188 00:08:48,300 --> 00:08:50,220 and at runtime it'll realize that 189 00:08:50,220 --> 00:08:52,530 and it'll call the employee print. 190 00:08:52,530 --> 00:08:54,660 No need for us to actually make any decisions ourself. 191 00:08:54,660 --> 00:08:56,970 It'll automatically figure out at runtime 192 00:08:56,970 --> 00:08:58,230 what was passed in. 193 00:08:58,230 --> 00:09:01,470 And then as we saw a polymorphic collection 194 00:09:01,470 --> 00:09:04,680 a vector of any image implements that trait 195 00:09:04,680 --> 00:09:08,223 such as the point object and the employee object. 196 00:09:09,180 --> 00:09:12,420 As I iterate through here at runtime, first time through 197 00:09:12,420 --> 00:09:14,580 it'll print out a point. 198 00:09:14,580 --> 00:09:17,970 Second time around the loop, it'll print out an employee. 199 00:09:17,970 --> 00:09:19,270 So let's run this and see. 200 00:09:21,180 --> 00:09:23,433 Okay, so, 201 00:09:24,360 --> 00:09:26,793 cargo run. 202 00:09:35,280 --> 00:09:38,730 Right, so in print something it printed a point first 203 00:09:38,730 --> 00:09:41,940 then an employee, okay. 204 00:09:41,940 --> 00:09:46,720 So here is a point 205 00:09:48,600 --> 00:09:50,703 and here is an employee. 206 00:09:52,770 --> 00:09:56,763 And then printable things in a polymorphic collection. 207 00:09:57,660 --> 00:09:58,770 That's my header. 208 00:09:58,770 --> 00:10:01,170 Printable things in a polymorphic collection. 209 00:10:01,170 --> 00:10:02,640 So it activates round 210 00:10:02,640 --> 00:10:05,400 and it prints out the point and the employee. 211 00:10:05,400 --> 00:10:07,920 So there's the point print function 212 00:10:07,920 --> 00:10:10,620 and there's the employee print function. 213 00:10:10,620 --> 00:10:11,453 Okay? 214 00:10:11,453 --> 00:10:15,090 So this is polymorphism and inheritance in Rust. 215 00:10:15,090 --> 00:10:18,480 You can't, inheritance has nothing to do with structures. 216 00:10:18,480 --> 00:10:20,430 It's all about traits. 217 00:10:20,430 --> 00:10:23,340 A structure can implement a trait in different ways 218 00:10:23,340 --> 00:10:24,600 and it's the trait 219 00:10:24,600 --> 00:10:28,140 that you use as your polymorphic superclass, okay? 220 00:10:28,140 --> 00:10:31,680 You can't pass in substitutable structures. 221 00:10:31,680 --> 00:10:33,750 It only works with traits. 222 00:10:33,750 --> 00:10:37,350 You can pass in anything which implements that trait. 223 00:10:37,350 --> 00:10:40,233 So it's quite different from most other languages.