Move supports two equality operations
|not equal||Returns |
Both the equal (
==) and not-equal (
!=) operations only work if both operands are the same type
Equality and non-equality also work over user defined types!
If the operands have different types, there is a type checking error
When comparing references, the type of the reference does not matter. This means that you can compare an immutable
& reference with a mutable one
&mut of the same type.
The above is equivalent to applying an explicit freeze to each mutable reference where needed
But similar to non-reference types, the underlying type must be the same type
!= consume the value when comparing them. As a result, the type system enforces that the type must be
copyable, that is that it is not a
resource value. Recall that resources cannot be copied, ownership must be transferred by the end of the function, and they can only be explicitly destroyed within their declaring module. If resources were used directly with either equality
== or non-equality
!=, the value would be destroyed which would break resource safety!
But, a programmer can always borrow the value first--to make it a
copyable type--instead of directly comparing the resource. For example
While a programmer can compare any
copyable value, a programmer should often compare by reference to avoid expensive copies.
This code is perfectly acceptable, just not efficient. The highlighted copies can be removed and replaced with borrows
The efficiency of the
== itself remains the same, but the
copys are removed and thus the program is more efficient.