1 00:00:06,140 --> 00:00:13,020 A trait represents a capability, something that a type can do and can be shared with other types. 2 00:00:13,040 --> 00:00:17,230 We can use traits to define shared behavior in an abstract way. 3 00:00:17,240 --> 00:00:20,840 So let's create a trait just to see how it's done. 4 00:00:22,010 --> 00:00:26,520 So all we have to do is use the trait keyword followed by the name we want our trait to be. 5 00:00:26,520 --> 00:00:28,860 So we're going to create an overview trait. 6 00:00:29,190 --> 00:00:32,880 And in this trait we're going to define a function. 7 00:00:34,580 --> 00:00:35,990 Hold overview. 8 00:00:38,480 --> 00:00:40,880 That is going to refer to itself. 9 00:00:43,340 --> 00:00:44,960 And return a string. 10 00:00:46,760 --> 00:00:48,710 And then to use this trade. 11 00:00:50,550 --> 00:00:54,960 You can actually implement it in with a type. 12 00:00:54,960 --> 00:00:56,430 So let's create a struct. 13 00:00:57,340 --> 00:00:59,200 And we're going to call it course. 14 00:01:01,950 --> 00:01:07,680 And then in here we're going to have a headline which is a string, and then the author, which is also 15 00:01:07,680 --> 00:01:08,340 a string. 16 00:01:09,460 --> 00:01:13,300 And then I'm going to go ahead and create another course. 17 00:01:15,440 --> 00:01:19,160 Is going to implement the same two things. 18 00:01:20,850 --> 00:01:27,540 So in here, what we're going to do is we're going to implement overview for course. 19 00:01:31,290 --> 00:01:35,100 And we're going to say an overview. 20 00:01:38,380 --> 00:01:46,600 So if we're going to return a string and then in here, we're going to say we're just going to format 21 00:01:46,600 --> 00:01:47,530 out. 22 00:01:49,170 --> 00:01:49,950 And then we're just. 23 00:01:49,950 --> 00:01:51,510 We just want to. 24 00:01:52,880 --> 00:01:54,110 To self. 25 00:01:55,160 --> 00:01:59,570 Dot author and self dot headline. 26 00:01:59,570 --> 00:02:01,730 So just something really easy for us to do. 27 00:02:01,730 --> 00:02:03,950 And then we want to do the same thing. 28 00:02:04,930 --> 00:02:05,770 For. 29 00:02:07,540 --> 00:02:08,500 Another course. 30 00:02:10,490 --> 00:02:16,280 So as you can see, we were able to implement the overview trait for two different types, which is 31 00:02:16,280 --> 00:02:21,350 exactly what we want to be able to do with the trait and exactly what the definition of a trait is. 32 00:02:21,500 --> 00:02:28,580 But something that we can do is we can actually have a default implementation for our trait. 33 00:02:29,560 --> 00:02:36,790 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. 34 00:02:39,140 --> 00:02:46,970 So now if we come down here, let's create a couple of our structs and we'll say Let. 35 00:02:48,550 --> 00:02:49,050 Course. 36 00:02:49,050 --> 00:02:51,510 One equal to course. 37 00:02:52,660 --> 00:03:02,110 And then we're going to have our headline is a string from headline. 38 00:03:04,240 --> 00:03:12,250 And then our author is a string from Tyler. 39 00:03:16,850 --> 00:03:18,680 And then we want to do the same thing again. 40 00:03:21,160 --> 00:03:25,000 Except this time we want it to be for another course. 41 00:03:29,380 --> 00:03:34,960 Another course and we'll say another headline. 42 00:03:36,350 --> 00:03:38,300 And another. 43 00:03:39,380 --> 00:03:39,900 Tyler. 44 00:03:39,920 --> 00:03:40,520 All right. 45 00:03:40,910 --> 00:03:41,480 So good. 46 00:03:41,870 --> 00:03:43,760 So we have no airs there so far. 47 00:03:43,760 --> 00:03:53,180 So let's create a print and say course one dot overview. 48 00:03:53,630 --> 00:03:58,040 So we're referencing we're calling the trait that we implemented. 49 00:03:58,580 --> 00:03:59,930 And then in here. 50 00:04:02,550 --> 00:04:09,270 Let's say, of course, to dot overview. 51 00:04:09,930 --> 00:04:11,160 And now let's run it. 52 00:04:12,790 --> 00:04:15,880 So we see Tyler headline. 53 00:04:16,680 --> 00:04:19,460 Which is what we implemented up here for course. 54 00:04:19,470 --> 00:04:24,660 And then we also see another Tyler, another headline, which is what? 55 00:04:25,950 --> 00:04:30,050 We the the values that we gave down here. 56 00:04:30,060 --> 00:04:33,690 So now how can we use our default thing? 57 00:04:34,110 --> 00:04:36,390 Because we currently are overriding it. 58 00:04:36,390 --> 00:04:40,620 So let's just go ahead and delete all of this. 59 00:04:43,370 --> 00:04:48,810 So now we just say implement overview for course, and we took out our implementation for it. 60 00:04:48,830 --> 00:04:56,360 So now if we run it, we actually see this is a rust course, which is the default implementation for 61 00:04:56,360 --> 00:04:59,330 it, and that's how traits work. 62 00:04:59,330 --> 00:05:03,500 So let's see what else that we can do with traits in the coming lectures.