Operating overloading is how we can make our own type support arithmetic.
In order to show these, we are going to show how to add coordinates together of any type.
So the first thing we need to do is bring in add into scope and it is in standard ops add.
So now we're going to create our stroke of a point with a generic type and assign to fields in here
all of the type t.
So now we can implement ADD.
Four point.
And then in here, we're going to do something we haven't done yet, but we're going to say where t
of.
Add
help put equals t.
And I'll break all this down in a second.
And now f in ad self right hand side self
return self.
So we added this where in here and what that is going to do is it's going to restrict t to types that
can be added to themselves yielding another T value.
So in my example, what we're going to what I'm going to want to do is.
Take two coordinates so we'll say let.
Cord equals point of x 5.0 and then y 5.0.
And then we'll copy and paste this to make it go quicker
and we'll just add one and two.
So when I talk when I was talking about.
Restricting T to types that can be added to themselves.
I'm just saying like we want to add a float to a float already.
That way we keep this example nice and simple just to really show what we're trying to do here, which
is implement adding in adding points, which is our custom type.
So now let's implement the logic for adding here.
So we're going to say point and then we're going to have X is going to be equal to self dot x.
So the left hand side of the equation.
So just to show this, we're going to say let sum equals chord plus core two.
So this court is left hand side and this core two is right hand side.
So we're going to say self dot x, which is left hand side plus self dot y, which is going to be the
right hand side.
And then also the same thing for y.
So thought y for left hand side plus right hand side.
So if y.
That's all we have to do.
So now let's just go ahead and print this out.
And we're going to need to use the debug form of printing and we want to print out the sum.
Now, since we haven't implemented in how to print out this, we actually need to derive in.
So we're going to use drive
and then debug and it went ahead and added that information.
I can take it out.
So now we'll be able to print this out using the debug formatter so when we cargo run it.
Value used here after move and.
That is.
Because I needed it to be right hand side.
There we go.
Now we should be good.
Right hand side x.
And now we see we have printed out a point with six as the X coordinate and Y as the seven coordinate,
which that looks correct.
So what we were able to do here was add use the add trait to implement arithmetic for our own unique
type, which was a point.
But this logic, we can apply to all arithmetic operators.
It was just something I wanted to show you how we could use the ADD in order to implement our own operations
for our custom type.