1 00:00:00,000 --> 00:00:07,133 [No Audio] 2 00:00:07,134 --> 00:00:09,500 Associated types in Rust are similar to 3 00:00:09,501 --> 00:00:12,233 generic types. However, associated types and 4 00:00:12,234 --> 00:00:14,566 limit the types of things a user can do, 5 00:00:14,833 --> 00:00:16,866 which consequently facilitates code 6 00:00:16,867 --> 00:00:19,533 management. They are related to the idea of 7 00:00:19,534 --> 00:00:22,200 type family, in other words, grouping 8 00:00:22,201 --> 00:00:25,000 multiple types together. That description is 9 00:00:25,001 --> 00:00:27,266 a bit abstract, so let's dive into it right 10 00:00:27,267 --> 00:00:30,866 now. I want to calculate how far my car will 11 00:00:30,867 --> 00:00:32,800 travel in three hours if it's going at a 12 00:00:32,801 --> 00:00:35,333 certain speed. I'm going to define a couple 13 00:00:35,334 --> 00:00:37,866 of structs for this purpose, with the names of 14 00:00:38,066 --> 00:00:41,733 Kmh, that is kilometer per hour, and Km, 15 00:00:41,766 --> 00:00:44,200 that is kilometers, each containing a single 16 00:00:44,201 --> 00:00:46,933 field of u32, and we'll derive the Debug. 17 00:00:46,934 --> 00:00:57,133 [No Audio] 18 00:00:57,134 --> 00:00:59,300 Defining structs with single fields like this 19 00:00:59,301 --> 00:01:01,200 is a great way to protect and safeguard 20 00:01:01,201 --> 00:01:03,666 against programming errors. Now it's 21 00:01:03,667 --> 00:01:06,466 impossible to accidentally compare Kmh, that 22 00:01:06,467 --> 00:01:09,866 is kilometer per hour against Km or kilometers. 23 00:01:09,867 --> 00:01:12,100 [No Audio] 24 00:01:12,101 --> 00:01:13,733 This is fine so far, but in some 25 00:01:13,734 --> 00:01:15,900 places, people are more familiar with miles 26 00:01:15,901 --> 00:01:18,800 per hour and miles, instead of kilometer per 27 00:01:18,801 --> 00:01:21,500 hours and kilometers. To cater for those 28 00:01:21,501 --> 00:01:23,266 people, we will also need to include 29 00:01:23,267 --> 00:01:25,100 implementation for the miles and miles per 30 00:01:25,101 --> 00:01:27,233 hour. So let me include a couple of more 31 00:01:27,234 --> 00:01:29,200 structs for the Miles per hour and Miles, 32 00:01:29,201 --> 00:01:32,866 having the similar syntax to that of Kmh and Km. 33 00:01:32,867 --> 00:01:42,500 [No Audio] 34 00:01:42,501 --> 00:01:44,700 Next, I am interested in determining how far 35 00:01:44,701 --> 00:01:47,033 my car has gone in three hours. So, let us 36 00:01:47,034 --> 00:01:48,933 first implement this four kilometer per hour 37 00:01:48,934 --> 00:01:50,666 inside an implementation block. 38 00:01:50,667 --> 00:01:55,933 [No Audio] 39 00:01:55,934 --> 00:01:57,700 The input to the function will be self, and the 40 00:01:57,701 --> 00:02:00,000 output will be the distance in kilometers, so 41 00:02:00,001 --> 00:02:01,400 it will be of type Km. 42 00:02:01,401 --> 00:02:06,900 [No Audio] 43 00:02:06,901 --> 00:02:09,233 Inside the function, we will return kilometers 44 00:02:09,532 --> 00:02:11,566 with a value being equal to the speed 45 00:02:11,567 --> 00:02:13,033 multiplied by three. 46 00:02:13,034 --> 00:02:17,833 [No Audio] 47 00:02:17,834 --> 00:02:19,800 This means that the distance covered in three 48 00:02:19,801 --> 00:02:21,900 hours will be the speed in kilometer per hour 49 00:02:21,901 --> 00:02:24,600 times three. Please note that self.value 50 00:02:24,601 --> 00:02:26,800 in this case, will be the speed since self 51 00:02:26,801 --> 00:02:29,400 represents the kilometer per hour instance. 52 00:02:29,900 --> 00:02:31,533 In the same way, we will add a function for 53 00:02:31,534 --> 00:02:33,366 computing the distance in three hours to the 54 00:02:33,367 --> 00:02:35,966 implementation block of miles per hour. 55 00:02:35,967 --> 00:02:38,133 [No Audio] 56 00:02:38,134 --> 00:02:39,666 The definition of the function will be 57 00:02:39,667 --> 00:02:41,533 similar to the above function, but in this 58 00:02:41,534 --> 00:02:44,300 case, it will return miles instead of kilometers. 59 00:02:44,301 --> 00:02:50,300 [No Audio] 60 00:02:50,301 --> 00:02:52,300 Let us use these functions in the main. I will 61 00:02:52,301 --> 00:02:54,833 declare a variable of speed having a type of 62 00:02:54,933 --> 00:02:57,733 Kmh or kilometer per hour with a value of 90. 63 00:02:57,734 --> 00:03:03,100 [No Audio] 64 00:03:03,101 --> 00:03:05,100 Next, I will declare another variable which 65 00:03:05,101 --> 00:03:07,266 will compute the distance in three hours by 66 00:03:07,267 --> 00:03:08,433 calling the function. 67 00:03:08,434 --> 00:03:13,266 [No Audio] 68 00:03:13,267 --> 00:03:15,366 Finally, I will display the two variables 69 00:03:15,367 --> 00:03:16,433 in a print statement. 70 00:03:16,434 --> 00:03:23,100 [No Audio] 71 00:03:23,101 --> 00:03:25,033 I will use the same three lines for the miles 72 00:03:25,034 --> 00:03:27,600 per hour now. So I will copy and paste, and 73 00:03:27,601 --> 00:03:29,700 we'll make changes in the variable names. 74 00:03:29,701 --> 00:03:35,400 [No Audio] 75 00:03:35,401 --> 00:03:36,966 Let us cargo run this. 76 00:03:36,967 --> 00:03:41,200 [No Audio] 77 00:03:41,201 --> 00:03:42,933 The code works and is 78 00:03:42,934 --> 00:03:45,900 doing our job. It feels a bit weird that we 79 00:03:45,901 --> 00:03:47,833 have these two almost mirror 80 00:03:47,834 --> 00:03:50,400 implementations without unifying them, it 81 00:03:50,401 --> 00:03:52,600 seems like a perfect use case for a trade. 82 00:03:53,533 --> 00:03:55,933 Let us look closely at the implementation of 83 00:03:55,934 --> 00:03:59,066 the Kmh and Mph. You may note that both 84 00:03:59,067 --> 00:04:00,800 implementations share the function of 85 00:04:00,801 --> 00:04:02,700 distance in three hours, and both the 86 00:04:02,701 --> 00:04:04,666 function returns a distance, although having 87 00:04:04,667 --> 00:04:06,866 different types. This means that, there is 88 00:04:06,867 --> 00:04:08,300 something which is shared among the two 89 00:04:08,301 --> 00:04:11,233 types. As I already covered in the course, 90 00:04:11,700 --> 00:04:14,100 that we use traits for sharing behavior among 91 00:04:14,101 --> 00:04:16,200 the types. Let me comment out these 92 00:04:16,201 --> 00:04:18,899 implementations and define a trait called a 93 00:04:18,900 --> 00:04:20,565 DistanceThreeHours. 94 00:04:20,566 --> 00:04:28,600 [No Audio] 95 00:04:28,601 --> 00:04:30,300 I will include a single function in this 96 00:04:30,301 --> 00:04:32,266 trait called, DistanceThreeHours. 97 00:04:32,267 --> 00:04:36,500 [No Audio] 98 00:04:36,501 --> 00:04:38,800 The input will be self, what about the output 99 00:04:38,801 --> 00:04:42,400 now? Should it be kilometers or miles? If I 100 00:04:42,401 --> 00:04:44,566 restrict it to kilometers, then the 101 00:04:44,567 --> 00:04:46,500 implementation for the Kmh 102 00:04:46,501 --> 00:04:48,633 will have no issue, but we will run into 103 00:04:48,634 --> 00:04:50,600 issues when we do implementation of the same 104 00:04:50,601 --> 00:04:54,000 for Mph. There must be some unifying type 105 00:04:54,001 --> 00:04:56,566 here, which based on the implementation will 106 00:04:56,567 --> 00:04:59,100 assume different types. Here comes the 107 00:04:59,101 --> 00:05:01,833 concept of associated types. I will declare 108 00:05:01,834 --> 00:05:04,466 a new associated type called distance before 109 00:05:04,467 --> 00:05:06,966 the function name. The output of this function 110 00:05:06,967 --> 00:05:10,433 will now be of type Distance. The Distance is 111 00:05:10,434 --> 00:05:14,100 now a new trait item. This type is associated 112 00:05:14,101 --> 00:05:16,966 with the type we are defining the trait for. 113 00:05:17,600 --> 00:05:19,466 We refer to it in the body of the trait 114 00:05:19,467 --> 00:05:22,266 definition via Self::Distance, 115 00:05:22,267 --> 00:05:27,066 therefore the term self here. Let us now 116 00:05:27,067 --> 00:05:29,800 implement the same trait for the Kmh. 117 00:05:29,801 --> 00:05:32,333 We will first include the implementation block. 118 00:05:32,334 --> 00:05:37,066 [No Audio] 119 00:05:37,067 --> 00:05:38,300 Next, we will declare the 120 00:05:38,301 --> 00:05:40,466 distance to be of type Kilometers. 121 00:05:40,467 --> 00:05:45,600 [No Audio] 122 00:05:45,601 --> 00:05:47,800 Next, we will declare the function of 123 00:05:47,801 --> 00:05:49,933 distance in three hours with the input of 124 00:05:49,934 --> 00:05:52,266 Self and the output of Self::Distance. 125 00:05:53,700 --> 00:05:57,300 Inside the function, we will return Self::Distance, 126 00:05:57,301 --> 00:05:59,366 and the value will be set to that 127 00:05:59,367 --> 00:06:01,233 of self.value times three. 128 00:06:01,234 --> 00:06:07,366 [No Audio] 129 00:06:07,367 --> 00:06:08,933 In the same way, we can go ahead and 130 00:06:08,934 --> 00:06:11,133 implement the distance in three hours for the 131 00:06:11,134 --> 00:06:14,000 Miles, I will first into the implementation block. 132 00:06:14,001 --> 00:06:20,366 [No Audio] 133 00:06:20,367 --> 00:06:22,300 Next, I will copy the code above, and we'll 134 00:06:22,301 --> 00:06:24,366 paste it, and now I will just change the 135 00:06:24,367 --> 00:06:26,200 kilometers to that of Miles. 136 00:06:26,201 --> 00:06:29,700 [No Audio] 137 00:06:29,701 --> 00:06:32,566 In the first implementation, which is for the Kmh, 138 00:06:32,567 --> 00:06:34,966 by declaring that type distance 139 00:06:34,967 --> 00:06:37,500 equals to Km, we are basically saying 140 00:06:37,501 --> 00:06:40,400 that, that the distance type associated with a 141 00:06:40,401 --> 00:06:43,300 kilometer per hour is kilometer. And in the 142 00:06:43,301 --> 00:06:45,400 same way, in the second implementation block 143 00:06:45,401 --> 00:06:47,833 by declaring the type Distance = Miles. 144 00:06:47,834 --> 00:06:49,700 We are basically saying that, the 145 00:06:49,701 --> 00:06:51,933 Distance type associated with the miles per 146 00:06:51,934 --> 00:06:54,766 hour is Miles. This means that different 147 00:06:54,767 --> 00:06:57,233 implementation blocks attaches different 148 00:06:57,234 --> 00:06:59,966 meanings to the distance, and therefore allows 149 00:06:59,967 --> 00:07:02,400 for having some flexibility, and a way of 150 00:07:02,401 --> 00:07:05,666 organizing our code more effectively. Inside 151 00:07:05,667 --> 00:07:07,400 the main function, you may note that the 152 00:07:07,401 --> 00:07:09,566 distance variable in the first case has been 153 00:07:09,600 --> 00:07:11,666 automatically assigned the correct type of 154 00:07:11,667 --> 00:07:14,666 kilometers, and in the second case, it has 155 00:07:14,667 --> 00:07:16,933 been automatically assigned the type of Miles 156 00:07:16,966 --> 00:07:20,166 which we intend to do. That is it for this 157 00:07:20,167 --> 00:07:22,266 particular tutorial. Do come back again and 158 00:07:22,267 --> 00:07:24,500 until then, happy Rust programming. 159 00:07:24,501 --> 00:07:29,533 [No Audio]