1 00:00:00,000 --> 00:00:08,400 [No Audio] 2 00:00:08,401 --> 00:00:10,500 In this tutorial, we will be learning about 3 00:00:10,501 --> 00:00:12,600 some very frequently used traits called 4 00:00:12,601 --> 00:00:16,033 partial equal and partial order traits. So 5 00:00:16,034 --> 00:00:18,500 let's start learning. These two traits are 6 00:00:18,501 --> 00:00:21,200 used for checking for equality and ordering. 7 00:00:21,900 --> 00:00:23,900 There is a whole bunch of mathematical theory 8 00:00:23,901 --> 00:00:26,100 related to the properties of relations such 9 00:00:26,101 --> 00:00:29,866 as reflexivity, symmetry, and transitivity. 10 00:00:30,100 --> 00:00:32,299 The details aren't vital to our 11 00:00:32,300 --> 00:00:34,600 understanding, so we're going to skip over them. 12 00:00:35,100 --> 00:00:37,400 If you are curious, you can search a 13 00:00:37,401 --> 00:00:39,633 bit about these topics such as partial order, 14 00:00:39,634 --> 00:00:42,633 total order, and equivalence relations. In 15 00:00:42,634 --> 00:00:44,733 simple words, this enables us to compare 16 00:00:44,900 --> 00:00:47,133 different instances of the struct with one 17 00:00:47,134 --> 00:00:50,633 another based on its fields. Let us learn the 18 00:00:50,634 --> 00:00:52,700 details by defining a simple struct called 19 00:00:52,701 --> 00:00:56,400 person having four fields including a name, 20 00:00:56,401 --> 00:00:58,100 age, earning, and saving. 21 00:00:58,101 --> 00:01:07,200 [No Audio] 22 00:01:07,201 --> 00:01:09,966 Let's create an instance of the struct Person called bob. 23 00:01:09,967 --> 00:01:17,766 [No Audio] 24 00:01:17,767 --> 00:01:19,666 Let us make a clone of the bob. 25 00:01:19,667 --> 00:01:24,900 [No Audio] 26 00:01:24,901 --> 00:01:27,066 You may notice that the compiler is not happy, 27 00:01:27,067 --> 00:01:29,333 let us ignore this for now and compare 28 00:01:29,334 --> 00:01:32,100 the instances of the struct in inside 29 00:01:32,101 --> 00:01:33,800 the print statement using the comparison 30 00:01:33,801 --> 00:01:36,700 operators of equality greater than or equal 31 00:01:36,701 --> 00:01:38,133 to and less than or equal to. 32 00:01:38,134 --> 00:01:46,400 [No Audio] 33 00:01:46,401 --> 00:01:48,700 You may note that the compiler is clearly not 34 00:01:48,701 --> 00:01:51,100 happy, this is because these operations are 35 00:01:51,101 --> 00:01:53,000 not defined for the type that we are 36 00:01:53,001 --> 00:01:55,800 considering, which in this case is the Person 37 00:01:55,801 --> 00:01:57,433 type, we need to therefore 38 00:01:57,434 --> 00:01:58,800 derive them for the struct. 39 00:01:58,801 --> 00:02:07,000 [No Audio] 40 00:02:07,001 --> 00:02:09,699 Everything seems fine now, let us cargo run this. 41 00:02:09,700 --> 00:02:15,900 [No Audio] 42 00:02:15,901 --> 00:02:18,100 All the statements are returned to be true in 43 00:02:18,101 --> 00:02:20,633 this case, let us look at these cases one by 44 00:02:20,634 --> 00:02:23,633 one, the equality symbol. If all the fields 45 00:02:23,700 --> 00:02:26,000 of the two structs are equal, then the two 46 00:02:26,001 --> 00:02:28,633 instances should be equal, we can change this 47 00:02:28,634 --> 00:02:30,433 default implementation to our own 48 00:02:30,434 --> 00:02:33,133 implementation. Let us redefine the 49 00:02:33,134 --> 00:02:35,300 partially equal trait by modifying the 50 00:02:35,301 --> 00:02:37,233 definition to, let's say the two instances 51 00:02:37,234 --> 00:02:40,166 will be only equal, when their ages are equal. 52 00:02:40,900 --> 00:02:42,900 Let us write the code for implementing the 53 00:02:42,901 --> 00:02:45,933 partial equal for person, this state has only 54 00:02:45,934 --> 00:02:48,700 one function called equal which has two inputs. 55 00:02:48,701 --> 00:02:56,200 [No Audio] 56 00:02:56,201 --> 00:02:58,500 The first input is a reference to self, and 57 00:02:58,501 --> 00:03:00,733 the second one is a reference to the same type. 58 00:03:00,734 --> 00:03:04,933 [No Audio] 59 00:03:04,934 --> 00:03:06,733 The output is a bool value. 60 00:03:08,300 --> 00:03:10,733 Inside the function, we will return the result of 61 00:03:10,734 --> 00:03:14,833 comparing the values of self.age with the other.age. 62 00:03:14,834 --> 00:03:20,233 [No Audio] 63 00:03:20,234 --> 00:03:22,700 Once this is done, we will not need to derive 64 00:03:22,701 --> 00:03:24,300 the partial equal for the type. 65 00:03:24,301 --> 00:03:30,300 [No Audio] 66 00:03:30,301 --> 00:03:32,933 Let us change the age of bob_clone 67 00:03:32,934 --> 00:03:34,500 to that of 15 and cargo run. 68 00:03:34,501 --> 00:03:42,400 [No Audio] 69 00:03:42,401 --> 00:03:44,966 You may note that, we have our files for the 70 00:03:45,000 --> 00:03:47,700 equality comparison. In this case now, only 71 00:03:47,701 --> 00:03:50,333 the ages fields of the two instances needs to 72 00:03:50,334 --> 00:03:52,700 have the same value instead of all the fields. 73 00:03:53,400 --> 00:03:55,300 Now, let us look at greater than or 74 00:03:55,301 --> 00:03:57,733 equal to, and less than or equal to. In the 75 00:03:57,734 --> 00:04:00,833 default implementation, partial order will 76 00:04:00,834 --> 00:04:04,400 check the greater than or equal, or equal, or 77 00:04:04,401 --> 00:04:07,000 less than or equal to in sequential order of 78 00:04:07,001 --> 00:04:09,200 the fields. This means that the fields of the 79 00:04:09,201 --> 00:04:12,133 struct will be checked one by one. In case of 80 00:04:12,134 --> 00:04:14,666 greater than or equal to, if the two fields 81 00:04:14,667 --> 00:04:17,399 are equal, then we will keep on checking the 82 00:04:17,400 --> 00:04:21,632 next field until they are equal or the first_value 83 00:04:21,633 --> 00:04:23,000 value is strictly greater 84 00:04:23,001 --> 00:04:25,600 than the second one. If this condition is 85 00:04:25,601 --> 00:04:27,933 met, then it will return a true, and if not 86 00:04:27,934 --> 00:04:31,000 then false. In this example, the names are 87 00:04:31,001 --> 00:04:32,933 equal, so we will check the second field 88 00:04:32,934 --> 00:04:35,300 which is age. Since the age of the bob is 89 00:04:35,301 --> 00:04:37,700 greater than that of the bob_clone, 90 00:04:37,701 --> 00:04:40,900 therefore it returns a true value. If the 91 00:04:40,901 --> 00:04:43,100 first value happens to be strictly lesser than 92 00:04:43,101 --> 00:04:45,533 the second value during these checks, then 93 00:04:45,534 --> 00:04:47,333 the next field will not be checked and a 94 00:04:47,334 --> 00:04:50,233 false value will be returned. Also note that 95 00:04:50,234 --> 00:04:52,500 for the textual information such as the names 96 00:04:52,501 --> 00:04:55,500 in this case, the alphabetical order dictates 97 00:04:55,501 --> 00:04:58,700 if a value is lesser or not. For instance, if 98 00:04:58,701 --> 00:05:00,600 I change the name of the bob_clone 99 00:05:00,601 --> 00:05:02,100 to that of Eve and cargo run. 100 00:05:02,101 --> 00:05:08,033 [No Audio] 101 00:05:08,034 --> 00:05:10,433 In this case, it has returned a false, because 102 00:05:10,434 --> 00:05:12,900 the first letter in the name of bob, which is 103 00:05:12,901 --> 00:05:16,000 b does not come after the first letter of e. 104 00:05:16,133 --> 00:05:18,700 In simple words, b is not greater than or 105 00:05:18,701 --> 00:05:22,300 equal to 3. The lesser than or equal to works 106 00:05:22,301 --> 00:05:24,533 in the same way, the fields will be checked 107 00:05:24,534 --> 00:05:26,600 one by one in sequential order, and if they 108 00:05:26,601 --> 00:05:29,000 are equal, then the next fields will be 109 00:05:29,001 --> 00:05:31,400 checked until the first value is strictly 110 00:05:31,401 --> 00:05:34,600 lesser than the second value. While doing 111 00:05:34,601 --> 00:05:36,733 these checks, if at any point the first value 112 00:05:36,734 --> 00:05:40,400 happens to be greater, then it will return a false. 113 00:05:41,166 --> 00:05:43,233 Instead of checking the partial equality 114 00:05:43,234 --> 00:05:46,400 this way, we can redefine the trait for our 115 00:05:46,401 --> 00:05:50,133 own type. Let us redefine it for our 116 00:05:50,134 --> 00:05:52,600 Person type using an implementation block. 117 00:05:52,601 --> 00:06:00,233 [No Audio] 118 00:06:00,234 --> 00:06:01,466 This function requires the 119 00:06:01,467 --> 00:06:04,133 partial_compare function, so, let us define it. 120 00:06:04,134 --> 00:06:06,933 [No Audio] 121 00:06:06,934 --> 00:06:09,633 This function has two inputs, the first one is a 122 00:06:09,634 --> 00:06:12,433 reference to self, and the 123 00:06:12,434 --> 00:06:15,000 second one is a reference to the self type, 124 00:06:15,700 --> 00:06:18,533 the output is an option ordering. The 125 00:06:18,534 --> 00:06:21,133 ordering is an enum which has three variants 126 00:06:21,134 --> 00:06:24,800 of less, equal, or greater. We will return the 127 00:06:24,801 --> 00:06:26,700 ordering by calling the partial compare 128 00:06:26,701 --> 00:06:29,800 function in the trait based on the earning variable. 129 00:06:29,801 --> 00:06:36,400 [No Audio] 130 00:06:36,401 --> 00:06:39,000 This means now, that we will be checking for 131 00:06:39,001 --> 00:06:40,600 lesser or greater than based on the 132 00:06:40,601 --> 00:06:42,900 variable of earning. We will also remove it 133 00:06:42,901 --> 00:06:46,100 from the derive, and we'll also bring the 134 00:06:46,101 --> 00:06:47,800 ordering enum into scope. 135 00:06:47,801 --> 00:06:53,900 [No Audio] 136 00:06:53,901 --> 00:06:55,700 Let us change the salary of the bob_clone 137 00:06:55,701 --> 00:06:57,366 to that of 40,000. 138 00:06:57,367 --> 00:07:00,233 [No Audio] 139 00:07:00,234 --> 00:07:01,466 The comparison in this case 140 00:07:01,467 --> 00:07:02,933 will now be only limited to the 141 00:07:02,934 --> 00:07:05,200 variable that we mentioned, and the 142 00:07:05,201 --> 00:07:07,133 next field will not be checked in case of 143 00:07:07,134 --> 00:07:09,500 equality. Let us execute this. 144 00:07:09,501 --> 00:07:13,666 [No Audio] 145 00:07:13,667 --> 00:07:15,333 It returned a false since 146 00:07:15,334 --> 00:07:18,033 bob_salary is not greater than his clone. 147 00:07:18,466 --> 00:07:20,100 The last point in this regard is 148 00:07:20,101 --> 00:07:22,200 that when we only use the greater than or 149 00:07:22,201 --> 00:07:24,833 less than without the equality, then the first 150 00:07:24,834 --> 00:07:27,000 fields are not checked in the default 151 00:07:27,001 --> 00:07:29,733 implementation. And if we have our own 152 00:07:29,734 --> 00:07:31,800 implementation, then only the field is being 153 00:07:31,801 --> 00:07:33,400 used, which we mentioned in the 154 00:07:33,401 --> 00:07:36,300 implementation. In case of equality, both the 155 00:07:36,301 --> 00:07:39,600 less than or greater than will return a false value. 156 00:07:40,400 --> 00:07:42,233 That is quite easy though, and 157 00:07:42,234 --> 00:07:45,000 therefore we will left that as an exercise. 158 00:07:45,800 --> 00:07:47,700 That brings us to the end of this tutorial. 159 00:07:47,800 --> 00:07:50,133 I hope you would have enjoyed this, do come back 160 00:07:50,134 --> 00:07:53,000 for more, and until then happy Rust programming. 161 00:07:53,001 --> 00:07:58,133 [No Audio]