In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use Rust built-in trait:PartialEq and Eq", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use Rust built-in trait:PartialEq and Eq"!
Rust uses traits in many places, from very simple operator overloading to the very subtle features of Send, Sync. Some traits can be automatically derived (you just need to write # [derive (Copy, Clone, PartialEq, Eq, Debug, Default, Hash,...)] You can get a magical implementation, and it's usually right.
The names of PartialEq and Eq Traits actually come from the equivalence relation and local equivalence relation in abstract algebra. In fact, there is only one difference between them, that is, whether they satisfy Reflexivity in the equality comparison.
PartialEq/// [`eq`]: PartialEq::eq/// [`ne`]: PartialEq::ne# [lang = "eq"] # [stable (feature = "rust1", since = "1.0.0")] # [doc (alias = "=")] # [doc (alias = "! =")] # [rustc_on_unimplemented (message = "can't compare `{Self} `with` {Rhs} `" Label = "no implementation for `{Self} = = {Rhs}`")] pub trait PartialEq {/ / This method tests for `self` and `other`values to be equal, and is used / / by `= =`. # [must_use] # [stable (feature = "rust1", since = "1.0.0")] fn eq (& self, other: & Rhs)-> bool; / / This method tests for `! =`. # [inline] # [must_use] # [stable (feature = "rust1", since = "1.0.0")] fn ne (& self, other: & Rhs)-> bool {! self.eq (other)}}
If we want to compare whether the two values x and y of a type are equal (unequal), for example: X = = y (x! = y), then we must implement PartialEq Trait for the type.
PartialEq can be implemented by the compiler using # [derive], where each field is compared when an struct makes an equality comparison, and if an enumeration is encountered, the data owned by the enumeration is also compared.
We can also implement PartialEq ourselves. We only need to implement the function fn eq (& self, other: & Self)-> bool to determine whether it is equal, and Rust will automatically provide fn ne (& self, other: & Self)-> bool. Examples are as follows:
Enum BookFormat {Paperback, Hardback, Ebook,} struct Book {isbn: i32, format: BookFormat,} impl PartialEq for Book {fn eq (& self, other: & Self)-> bool {self.isbn = = other.isbn}} Eqpub trait Eq: PartialEq {/ / this method is used solely by # [deriving] to assert / / that every component of a type implements # [deriving] / / itself The current deriving infrastructure means doing this / / assertion without using a method on this trait is nearly / / impossible. / This should never be implemented by hand. # [doc (hidden)] # [inline] # [stable (feature = "rust1", since = "1.0.0")] fn assert_receiver_is_total_eq (& self) {}}
The premise of implementing Eq is that PartialEq has been implemented, because no additional code is needed to implement Eq, just tell the compiler that its comparison is reflexive based on the implementation of PartialEq. For the above example, you only need: # [derive (Eq)] or impl Eq for Book {}.
Enum BookFormat {Paperback, Hardback, Ebook,} struct Book {isbn: i32, format: BookFormat,} impl PartialEq for Book {fn eq (& self, other: & Self)-> bool {self.isbn = = other.isbn}} impl Eq for Book {} PartialEq and Eq
The names of these two Traits actually come from the equivalence relation and local equivalence relation in abstract algebra.
Equivalence relation (equivalence relation) is to say that\ displaystyle RR is a binary relation on a set\ displaystyle AA. If\ displaystyle RR meets the following conditions:
Then it is said that\ displaystyle RR is an equivalent relation defined on\ displaystyle AA.
Not all binary relations are equivalent. The difference between Eq and PartialEq lies in whether they satisfy reflexivity in equality comparison, that is, x = = x.
For example, for floating-point types, Rust implements only PartialEq but not Eq because NaN! = Nan is not reflexive.
Eq requires extra reflexivity than PartialEq, that is, a = = a. For floating-point types, Rust only implements PartialEq instead of Eq, because NaN! = NaN.
Eq and Hash
When a type implements both Eq and Hash, the type satisfies the following characteristics:
K1 = = K2-> hash (K1) = = hash (K2)
That is, when two key are equal, their hashes must be equal. Both HashMap and HashSet in Rust rely on this feature.
At this point, I believe you have a deeper understanding of "how to use Rust built-in trait:PartialEq and Eq". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.