1 00:00:05,750 --> 00:00:09,080 I hope you enjoyed this section on trades and generics. 2 00:00:09,410 --> 00:00:15,200 We are now going to go over to the solution for the assignment for this section. 3 00:00:16,190 --> 00:00:22,670 Question one was modified the solution to the section for assignment by creating a trait that has the 4 00:00:22,670 --> 00:00:30,380 set and jpg, set color and set top speed methods, then create a motorcycle struct with the same fields 5 00:00:30,380 --> 00:00:31,370 as the car struct. 6 00:00:31,370 --> 00:00:35,930 And those fields were MMORPG color and top speed. 7 00:00:36,620 --> 00:00:42,110 Now we are going to implement our trait on both the car and the motorcycle struck and then print out 8 00:00:42,110 --> 00:00:44,780 the results to confirm a working solution. 9 00:00:46,070 --> 00:00:54,830 So the first thing that we will do is take our solution from the section for assignment and paste it 10 00:00:54,830 --> 00:00:55,250 in. 11 00:00:55,490 --> 00:01:03,080 That way we are set back up with where we were at and now we want to create a motorcycle struct. 12 00:01:03,410 --> 00:01:11,750 So I'm going to copy and paste the car struck and just rename it motorcycle and leave the fields the 13 00:01:11,750 --> 00:01:12,290 same. 14 00:01:14,050 --> 00:01:24,730 We are now going to create a trait and I'm going to call this properties, since these are properties 15 00:01:24,730 --> 00:01:31,120 of a motorcycle and a car, and now we can take our function, our methods. 16 00:01:31,120 --> 00:01:35,590 So I'm just going to go ahead and copy and paste all of these up here. 17 00:01:38,340 --> 00:01:40,380 And I could do that. 18 00:01:44,820 --> 00:01:48,540 So now we have our TRATE successfully set up. 19 00:01:49,700 --> 00:01:54,110 So now we want to implement our trait for each of our strokes. 20 00:01:54,110 --> 00:02:04,670 So we will say ample properties for car and we will leave our implementation right here, the same for 21 00:02:04,670 --> 00:02:05,330 CAR. 22 00:02:05,360 --> 00:02:08,750 And now we will also do the same. 23 00:02:09,930 --> 00:02:11,130 Four motorcycle. 24 00:02:14,070 --> 00:02:16,950 Now we can create our motorcycle struck. 25 00:02:23,280 --> 00:02:33,930 And we can also go ahead and now assign it are set values so we'll say our motorcycle gets better miles 26 00:02:33,930 --> 00:02:40,410 to the gallon we have a black color and our top speed is 150 and then we want our default color. 27 00:02:40,620 --> 00:02:42,240 We'll have it as green. 28 00:02:42,840 --> 00:02:45,870 So now we want to run it. 29 00:02:47,250 --> 00:02:48,150 And we see. 30 00:02:48,150 --> 00:02:50,040 We have printed out. 31 00:02:50,460 --> 00:02:54,670 Oop, I need to motorcycle. 32 00:02:55,320 --> 00:02:58,740 I guess this is where motorcycle here. 33 00:02:58,770 --> 00:03:00,660 I was going a little too quickly. 34 00:03:01,650 --> 00:03:02,820 Motorcycle. 35 00:03:04,320 --> 00:03:05,550 Motorcycle. 36 00:03:06,360 --> 00:03:07,410 Motorcycle. 37 00:03:08,730 --> 00:03:17,010 And now we can print out both of our strokes now that we have them successfully set up and we see that 38 00:03:17,010 --> 00:03:21,960 we now have our new motorcycle struck, printing out the appropriate fields above. 39 00:03:22,230 --> 00:03:25,770 So that is one possible solution to question one. 40 00:03:26,880 --> 00:03:33,270 Question two is create a simple print function that uses generic type T. 41 00:03:33,540 --> 00:03:40,530 This generic T will need to implement the standard format debug depending on the values you pass in. 42 00:03:41,070 --> 00:03:48,120 Our function takes one parameter that is of type T and then our function will print out the value that 43 00:03:48,120 --> 00:03:49,110 is passed in. 44 00:03:50,250 --> 00:03:57,420 So we can create our function called print that is of type T, and it's going to take one parameter 45 00:03:57,420 --> 00:04:08,700 which we will call value and we need to now implement in the standard format debug. 46 00:04:08,700 --> 00:04:17,190 So that way we are able to print out our value that is of type T, so now we can print out 47 00:04:20,100 --> 00:04:20,700 value. 48 00:04:22,350 --> 00:04:31,770 And so if we come down here, we can call our print function, our print function on a wide variety 49 00:04:31,770 --> 00:04:32,130 of things. 50 00:04:32,130 --> 00:04:35,370 So we'll create a vector and we'll pass in values one to. 51 00:04:36,660 --> 00:04:37,620 And three. 52 00:04:38,850 --> 00:04:44,610 We'll call our print function on a string slice. 53 00:04:46,230 --> 00:04:48,120 We'll call our print function. 54 00:04:49,860 --> 00:04:57,060 On a string from string number two. 55 00:04:59,190 --> 00:05:04,260 And then lastly, we just want to print out a simple integer. 56 00:05:04,260 --> 00:05:06,240 So we'll print out ten. 57 00:05:07,590 --> 00:05:09,180 And now if we run this. 58 00:05:10,180 --> 00:05:16,840 We see that we are able to successfully print out four different types of values thanks to the ability 59 00:05:16,840 --> 00:05:19,510 that we gain when we use generics. 60 00:05:19,750 --> 00:05:25,150 So I hope you were able to follow along and compare your solutions to mine. 61 00:05:25,180 --> 00:05:31,990 If you have any questions about any of the solutions, or maybe your solution that differs from mine, 62 00:05:31,990 --> 00:05:35,140 please feel free to ask them in the Q&A section.