1 00:00:05,690 --> 00:00:11,150 Wouldn't it be nice to be able to create code that can operate on values of many different types? 2 00:00:11,420 --> 00:00:14,150 Luckily we can, and it is through generics. 3 00:00:15,230 --> 00:00:18,740 Generics are abstract standards for concrete types. 4 00:00:19,100 --> 00:00:24,740 We've already used generics in this course, and it was in the last section when talking about options. 5 00:00:24,920 --> 00:00:28,170 So let's take a look at how we can use generics. 6 00:00:28,190 --> 00:00:36,800 So to start off, let's create a struct and we'll call it point and then we'll assign it a generic of 7 00:00:36,800 --> 00:00:41,120 type T, which can be any type. 8 00:00:41,120 --> 00:00:47,420 And then inside we'll have X, which is T and then Y, which is also T. 9 00:00:47,870 --> 00:00:54,290 So currently, whatever value we pass in to X and Y, they're going to have to be the same type. 10 00:00:54,290 --> 00:00:55,790 So for example. 11 00:00:56,900 --> 00:01:00,530 I 32 and then that's what t will be. 12 00:01:00,530 --> 00:01:02,720 So therefore this also needs to be high. 13 00:01:02,750 --> 00:01:07,610 32 So let's create a point. 14 00:01:11,680 --> 00:01:17,110 He will call it point and then X, we'll just name it five. 15 00:01:17,410 --> 00:01:21,490 So we're going to make it a float and we'll just do five oh again. 16 00:01:24,270 --> 00:01:25,860 And so that's perfectly valid. 17 00:01:25,860 --> 00:01:36,750 So we can also, if we wanted to, for whatever reason, wanted our point to be characters, well, 18 00:01:36,750 --> 00:01:44,280 we can say X and then we can also say Y, and that's also valid. 19 00:01:44,280 --> 00:01:48,840 So we can see that T can be of any type that we want it to be. 20 00:01:48,840 --> 00:01:55,710 It's just a placeholder until we're able to figure out what type. 21 00:01:57,130 --> 00:01:57,790 Is needed. 22 00:01:59,270 --> 00:02:04,220 So currently what we cannot do is mix and match these, so we cannot go. 23 00:02:06,000 --> 00:02:09,150 Character, for one, and then a float for another. 24 00:02:09,920 --> 00:02:13,250 But we actually can fix that and we can't allow it to do that. 25 00:02:13,250 --> 00:02:20,960 So we can add in another generic, which is going to be a you in this case and then add in a you there. 26 00:02:21,530 --> 00:02:26,420 And now we are able to have two different types in here. 27 00:02:26,660 --> 00:02:31,580 So now we're able to do this, oh, I don't want that. 28 00:02:32,690 --> 00:02:34,550 So now we can have a character. 29 00:02:35,850 --> 00:02:39,900 And a float in here because we're able to accept two different types. 30 00:02:40,350 --> 00:02:43,600 And truly, that's really all there is to it. 31 00:02:43,620 --> 00:02:48,120 For generics, generics are just going to be placeholders until we're able to. 32 00:02:49,150 --> 00:02:54,310 Fill in a concrete type and generics are really. 33 00:02:55,240 --> 00:03:02,830 Powerful in this sense because it allows us to be flexible and make our code a lot more modular for 34 00:03:02,830 --> 00:03:11,320 handling different concrete types such as I 32 or a float, you know, booleans, whatever it might 35 00:03:11,320 --> 00:03:11,790 be. 36 00:03:11,800 --> 00:03:17,350 So it just allows us to have a lot of flexibility, which we will be able to see as we continue on in 37 00:03:17,350 --> 00:03:18,040 this course.