I hope you enjoyed this section on trades and generics.
We are now going to go over to the solution for the assignment for this section.
Question one was modified the solution to the section for assignment by creating a trait that has the
set and jpg, set color and set top speed methods, then create a motorcycle struct with the same fields
as the car struct.
And those fields were MMORPG color and top speed.
Now we are going to implement our trait on both the car and the motorcycle struck and then print out
the results to confirm a working solution.
So the first thing that we will do is take our solution from the section for assignment and paste it
in.
That way we are set back up with where we were at and now we want to create a motorcycle struct.
So I'm going to copy and paste the car struck and just rename it motorcycle and leave the fields the
same.
We are now going to create a trait and I'm going to call this properties, since these are properties
of a motorcycle and a car, and now we can take our function, our methods.
So I'm just going to go ahead and copy and paste all of these up here.
And I could do that.
So now we have our TRATE successfully set up.
So now we want to implement our trait for each of our strokes.
So we will say ample properties for car and we will leave our implementation right here, the same for
CAR.
And now we will also do the same.
Four motorcycle.
Now we can create our motorcycle struck.
And we can also go ahead and now assign it are set values so we'll say our motorcycle gets better miles
to the gallon we have a black color and our top speed is 150 and then we want our default color.
We'll have it as green.
So now we want to run it.
And we see.
We have printed out.
Oop, I need to motorcycle.
I guess this is where motorcycle here.
I was going a little too quickly.
Motorcycle.
And now we can print out both of our strokes now that we have them successfully set up and we see that
we now have our new motorcycle struck, printing out the appropriate fields above.
So that is one possible solution to question one.
Question two is create a simple print function that uses generic type T.
This generic T will need to implement the standard format debug depending on the values you pass in.
Our function takes one parameter that is of type T and then our function will print out the value that
is passed in.
So we can create our function called print that is of type T, and it's going to take one parameter
which we will call value and we need to now implement in the standard format debug.
So that way we are able to print out our value that is of type T, so now we can print out
value.
And so if we come down here, we can call our print function, our print function on a wide variety
of things.
So we'll create a vector and we'll pass in values one to.
And three.
We'll call our print function on a string slice.
We'll call our print function.
On a string from string number two.
And then lastly, we just want to print out a simple integer.
So we'll print out ten.
And now if we run this.
We see that we are able to successfully print out four different types of values thanks to the ability
that we gain when we use generics.
So I hope you were able to follow along and compare your solutions to mine.
If you have any questions about any of the solutions, or maybe your solution that differs from mine,
please feel free to ask them in the Q&A section.