1 00:00:06,240 --> 00:00:10,470 Operating overloading is how we can make our own type support arithmetic. 2 00:00:10,590 --> 00:00:17,310 In order to show these, we are going to show how to add coordinates together of any type. 3 00:00:17,520 --> 00:00:26,730 So the first thing we need to do is bring in add into scope and it is in standard ops add. 4 00:00:27,810 --> 00:00:37,830 So now we're going to create our stroke of a point with a generic type and assign to fields in here 5 00:00:38,640 --> 00:00:41,520 all of the type t. 6 00:00:43,340 --> 00:00:46,190 So now we can implement ADD. 7 00:00:49,200 --> 00:00:50,280 Four point. 8 00:00:54,080 --> 00:01:00,740 And then in here, we're going to do something we haven't done yet, but we're going to say where t 9 00:01:01,550 --> 00:01:02,240 of. 10 00:01:04,140 --> 00:01:04,950 Add 11 00:01:07,830 --> 00:01:11,190 help put equals t. 12 00:01:14,640 --> 00:01:18,180 And I'll break all this down in a second. 13 00:01:23,580 --> 00:01:31,800 And now f in ad self right hand side self 14 00:01:33,480 --> 00:01:36,030 return self. 15 00:01:36,630 --> 00:01:45,780 So we added this where in here and what that is going to do is it's going to restrict t to types that 16 00:01:45,780 --> 00:01:49,980 can be added to themselves yielding another T value. 17 00:01:49,980 --> 00:01:54,630 So in my example, what we're going to what I'm going to want to do is. 18 00:01:56,090 --> 00:01:58,520 Take two coordinates so we'll say let. 19 00:01:58,550 --> 00:02:09,380 Cord equals point of x 5.0 and then y 5.0. 20 00:02:11,960 --> 00:02:15,740 And then we'll copy and paste this to make it go quicker 21 00:02:18,350 --> 00:02:22,000 and we'll just add one and two. 22 00:02:22,010 --> 00:02:24,860 So when I talk when I was talking about. 23 00:02:25,840 --> 00:02:28,620 Restricting T to types that can be added to themselves. 24 00:02:28,630 --> 00:02:32,770 I'm just saying like we want to add a float to a float already. 25 00:02:32,770 --> 00:02:38,140 That way we keep this example nice and simple just to really show what we're trying to do here, which 26 00:02:38,140 --> 00:02:44,890 is implement adding in adding points, which is our custom type. 27 00:02:45,460 --> 00:02:48,670 So now let's implement the logic for adding here. 28 00:02:48,670 --> 00:02:56,080 So we're going to say point and then we're going to have X is going to be equal to self dot x. 29 00:02:56,080 --> 00:02:58,120 So the left hand side of the equation. 30 00:02:58,120 --> 00:03:05,020 So just to show this, we're going to say let sum equals chord plus core two. 31 00:03:05,260 --> 00:03:09,940 So this court is left hand side and this core two is right hand side. 32 00:03:09,940 --> 00:03:19,180 So we're going to say self dot x, which is left hand side plus self dot y, which is going to be the 33 00:03:19,240 --> 00:03:20,380 right hand side. 34 00:03:22,180 --> 00:03:24,040 And then also the same thing for y. 35 00:03:24,040 --> 00:03:27,700 So thought y for left hand side plus right hand side. 36 00:03:27,700 --> 00:03:29,020 So if y. 37 00:03:30,380 --> 00:03:31,580 That's all we have to do. 38 00:03:31,640 --> 00:03:34,520 So now let's just go ahead and print this out. 39 00:03:38,930 --> 00:03:45,230 And we're going to need to use the debug form of printing and we want to print out the sum. 40 00:03:45,410 --> 00:03:53,420 Now, since we haven't implemented in how to print out this, we actually need to derive in. 41 00:03:54,830 --> 00:03:57,620 So we're going to use drive 42 00:04:00,140 --> 00:04:06,800 and then debug and it went ahead and added that information. 43 00:04:06,830 --> 00:04:07,550 I can take it out. 44 00:04:07,550 --> 00:04:12,380 So now we'll be able to print this out using the debug formatter so when we cargo run it. 45 00:04:15,550 --> 00:04:18,940 Value used here after move and. 46 00:04:21,190 --> 00:04:22,330 That is. 47 00:04:23,630 --> 00:04:27,170 Because I needed it to be right hand side. 48 00:04:28,250 --> 00:04:28,910 There we go. 49 00:04:31,550 --> 00:04:32,900 Now we should be good. 50 00:04:37,570 --> 00:04:39,310 Right hand side x. 51 00:04:42,060 --> 00:04:48,360 And now we see we have printed out a point with six as the X coordinate and Y as the seven coordinate, 52 00:04:48,360 --> 00:04:50,670 which that looks correct. 53 00:04:50,820 --> 00:04:59,100 So what we were able to do here was add use the add trait to implement arithmetic for our own unique 54 00:04:59,100 --> 00:05:00,750 type, which was a point. 55 00:05:01,980 --> 00:05:06,750 But this logic, we can apply to all arithmetic operators. 56 00:05:07,290 --> 00:05:16,800 It was just something I wanted to show you how we could use the ADD in order to implement our own operations 57 00:05:16,800 --> 00:05:18,420 for our custom type.