A trait represents a capability, something that a type can do and can be shared with other types.
We can use traits to define shared behavior in an abstract way.
So let's create a trait just to see how it's done.
So all we have to do is use the trait keyword followed by the name we want our trait to be.
So we're going to create an overview trait.
And in this trait we're going to define a function.
Hold overview.
That is going to refer to itself.
And return a string.
And then to use this trade.
You can actually implement it in with a type.
So let's create a struct.
And we're going to call it course.
And then in here we're going to have a headline which is a string, and then the author, which is also
a string.
And then I'm going to go ahead and create another course.
Is going to implement the same two things.
So in here, what we're going to do is we're going to implement overview for course.
And we're going to say an overview.
So if we're going to return a string and then in here, we're going to say we're just going to format
out.
And then we're just.
We just want to.
To self.
Dot author and self dot headline.
So just something really easy for us to do.
And then we want to do the same thing.
For.
Another course.
So as you can see, we were able to implement the overview trait for two different types, which is
exactly what we want to be able to do with the trait and exactly what the definition of a trait is.
But something that we can do is we can actually have a default implementation for our trait.
So in here we're going to say we have a string from and then we want it to say this is a rust course.
So now if we come down here, let's create a couple of our structs and we'll say Let.
Course.
One equal to course.
And then we're going to have our headline is a string from headline.
And then our author is a string from Tyler.
And then we want to do the same thing again.
Except this time we want it to be for another course.
Another course and we'll say another headline.
And another.
Tyler.
All right.
So good.
So we have no airs there so far.
So let's create a print and say course one dot overview.
So we're referencing we're calling the trait that we implemented.
And then in here.
Let's say, of course, to dot overview.
And now let's run it.
So we see Tyler headline.
Which is what we implemented up here for course.
And then we also see another Tyler, another headline, which is what?
We the the values that we gave down here.
So now how can we use our default thing?
Because we currently are overriding it.
So let's just go ahead and delete all of this.
So now we just say implement overview for course, and we took out our implementation for it.
So now if we run it, we actually see this is a rust course, which is the default implementation for
it, and that's how traits work.
So let's see what else that we can do with traits in the coming lectures.