Wouldn't it be nice to be able to create code that can operate on values of many different types?
Luckily we can, and it is through generics.
Generics are abstract standards for concrete types.
We've already used generics in this course, and it was in the last section when talking about options.
So let's take a look at how we can use generics.
So to start off, let's create a struct and we'll call it point and then we'll assign it a generic of
type T, which can be any type.
And then inside we'll have X, which is T and then Y, which is also T.
So currently, whatever value we pass in to X and Y, they're going to have to be the same type.
So for example.
I 32 and then that's what t will be.
So therefore this also needs to be high.
32 So let's create a point.
He will call it point and then X, we'll just name it five.
So we're going to make it a float and we'll just do five oh again.
And so that's perfectly valid.
So we can also, if we wanted to, for whatever reason, wanted our point to be characters, well,
we can say X and then we can also say Y, and that's also valid.
So we can see that T can be of any type that we want it to be.
It's just a placeholder until we're able to figure out what type.
Is needed.
So currently what we cannot do is mix and match these, so we cannot go.
Character, for one, and then a float for another.
But we actually can fix that and we can't allow it to do that.
So we can add in another generic, which is going to be a you in this case and then add in a you there.
And now we are able to have two different types in here.
So now we're able to do this, oh, I don't want that.
So now we can have a character.
And a float in here because we're able to accept two different types.
And truly, that's really all there is to it.
For generics, generics are just going to be placeholders until we're able to.
Fill in a concrete type and generics are really.
Powerful in this sense because it allows us to be flexible and make our code a lot more modular for
handling different concrete types such as I 32 or a float, you know, booleans, whatever it might
be.
So it just allows us to have a lot of flexibility, which we will be able to see as we continue on in
this course.