[No Audio]
In this tutorial, we will be learning about
some very frequently used traits called
partial equal and partial order traits. So
let's start learning. These two traits are
used for checking for equality and ordering.
There is a whole bunch of mathematical theory
related to the properties of relations such
as reflexivity, symmetry, and transitivity.
The details aren't vital to our
understanding, so we're going to skip over them.
If you are curious, you can search a
bit about these topics such as partial order,
total order, and equivalence relations. In
simple words, this enables us to compare
different instances of the struct with one
another based on its fields. Let us learn the
details by defining a simple struct called
person having four fields including a name,
age, earning, and saving.
[No Audio]
Let's create an instance of the struct Person called bob.
[No Audio]
Let us make a clone of the bob.
[No Audio]
You may notice that the compiler is not happy,
let us ignore this for now and compare
the instances of the struct in inside
the print statement using the comparison
operators of equality greater than or equal
to and less than or equal to.
[No Audio]
You may note that the compiler is clearly not
happy, this is because these operations are
not defined for the type that we are
considering, which in this case is the Person
type, we need to therefore
derive them for the struct.
[No Audio]
Everything seems fine now, let us cargo run this.
[No Audio]
All the statements are returned to be true in
this case, let us look at these cases one by
one, the equality symbol. If all the fields
of the two structs are equal, then the two
instances should be equal, we can change this
default implementation to our own
implementation. Let us redefine the
partially equal trait by modifying the
definition to, let's say the two instances
will be only equal, when their ages are equal.
Let us write the code for implementing the
partial equal for person, this state has only
one function called equal which has two inputs.
[No Audio]
The first input is a reference to self, and
the second one is a reference to the same type.
[No Audio]
The output is a bool value.
Inside the function, we will return the result of
comparing the values of self.age with the other.age.
[No Audio]
Once this is done, we will not need to derive
the partial equal for the type.
[No Audio]
Let us change the age of bob_clone
to that of 15 and cargo run.
[No Audio]
You may note that, we have our files for the
equality comparison. In this case now, only
the ages fields of the two instances needs to
have the same value instead of all the fields.
Now, let us look at greater than or
equal to, and less than or equal to. In the
default implementation, partial order will
check the greater than or equal, or equal, or
less than or equal to in sequential order of
the fields. This means that the fields of the
struct will be checked one by one. In case of
greater than or equal to, if the two fields
are equal, then we will keep on checking the
next field until they are equal or the first_value
value is strictly greater
than the second one. If this condition is
met, then it will return a true, and if not
then false. In this example, the names are
equal, so we will check the second field
which is age. Since the age of the bob is
greater than that of the bob_clone,
therefore it returns a true value. If the
first value happens to be strictly lesser than
the second value during these checks, then
the next field will not be checked and a
false value will be returned. Also note that
for the textual information such as the names
in this case, the alphabetical order dictates
if a value is lesser or not. For instance, if
I change the name of the bob_clone
to that of Eve and cargo run.
[No Audio]
In this case, it has returned a false, because
the first letter in the name of bob, which is
b does not come after the first letter of e.
In simple words, b is not greater than or
equal to 3. The lesser than or equal to works
in the same way, the fields will be checked
one by one in sequential order, and if they
are equal, then the next fields will be
checked until the first value is strictly
lesser than the second value. While doing
these checks, if at any point the first value
happens to be greater, then it will return a false.
Instead of checking the partial equality
this way, we can redefine the trait for our
own type. Let us redefine it for our
Person type using an implementation block.
[No Audio]
This function requires the
partial_compare function, so, let us define it.
[No Audio]
This function has two inputs, the first one is a
reference to self, and the
second one is a reference to the self type,
the output is an option ordering. The
ordering is an enum which has three variants
of less, equal, or greater. We will return the
ordering by calling the partial compare
function in the trait based on the earning variable.
[No Audio]
This means now, that we will be checking for
lesser or greater than based on the
variable of earning. We will also remove it
from the derive, and we'll also bring the
ordering enum into scope.
[No Audio]
Let us change the salary of the bob_clone
to that of 40,000.
[No Audio]
The comparison in this case
will now be only limited to the
variable that we mentioned, and the
next field will not be checked in case of
equality. Let us execute this.
[No Audio]
It returned a false since
bob_salary is not greater than his clone.
The last point in this regard is
that when we only use the greater than or
less than without the equality, then the first
fields are not checked in the default
implementation. And if we have our own
implementation, then only the field is being
used, which we mentioned in the
implementation. In case of equality, both the
less than or greater than will return a false value.
That is quite easy though, and
therefore we will left that as an exercise.
That brings us to the end of this tutorial.
I hope you would have enjoyed this, do come back
for more, and until then happy Rust programming.
[No Audio]