How to implement partialEq on a struct with vectors because of f64?

There aren't a lot of examples on the internet, but
how to implemenent partialeq on a vector?
how to compare vectors in the implementation?
when you can't use #[derive(PartialEq)]
how to do the following?

impl PartialEq for IfwoifisIfcalculoIfnumerare {
    fn eq(&self, other: &Self) -> bool {
        self.ifwoifis_ifunique == other.ifwoifis_ifunique &&
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IfwoifisIfcalculoIfnumerare {
    pub ifwoifis_ifnumerus: f64,
    pub ifwoifare_ifcalculo: Vec<IfwoifisIfcalculo> 
pub struct IfwoifisIfcalculo {
    pub ifwoifis_ifunique: String, 
    pub ifwoifare_ifoutputs: Option<Vec<IfwoifisIfinput>>,
    pub ifwoifare_ifinputs: Option<Vec<IfwoifisIfoutput>>
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IfwoifisIfinput {
    pub ifwoifis_ifcalculo_ifnumerare_ifid: Option<String>,
    pub ifwoifis_ifindex: i8,
    pub ifwoifis_ifsignature: String,
    #[serde(with = "ifwoifis_ifdate_ifformat")]
    pub ifwoifis_ifutc: chrono::DateTime<chrono::Utc>,

pub struct IfwoifisIfoutput {
    pub ifwoifis_ifpublic_ifkey: String,
    pub ifwoifshould_iflaunch: Option<bool>,

Is there a particular reason why you don’t just derive(PartialEq) on all the structs involved?

1 Like
binary operation `==` cannot be applied to type `std::option::Option<Vec<ifwoifare_ifcalculo_ifnumerare::ifwoifis_ifcalculo_ifnumerare::ifwoifis_ifinput::IfwoifisIfinput>>`

By the way, in case you’re curious, the (default) implementation from the standard library uses something like

self.len() == other.len() && self.iter().zip(other.iter()).all(|(x, y)| x == y)

for comparing two Vecs / slices.

If you have a Vec<SomeType> containing some type that itself implements PartialEq, i.e. SomeType: PartialEq, then you can just == on the vecs. If this isn’t the case or you need some custom logic, you can try to adapt the pattern and do something other than == inside of the .all() call.

1 Like

Right… make sure to click the playground link I added to my previous answer. (On the words “all the structs”.) Of course it doesn’t work if you don’t derive it for all the structs involved. Apparently your type ifwoifare_ifcalculo_ifnumerare::ifwoifis_ifcalculo_ifnumerare::ifwoifis_ifinput::IfwoifisIfinput doesn’t implement PartialEq.

does noah see now?
is because f64?

the trait bound `f64: Eq` is not satisfied
   --> rs/src/ifwoifare_ifcalculo_ifnumerare/ifwoifis_ifcalculo_ifnumerare/
29  |     pub ifwoifis_ifnumerus: f64,
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `f64`

You can't derive Eq, but you can derive PartialEq.

f64 doesn’t implement Eq because (NaN == NaN) returns false which violates the reflexivity (for all a: a == a) requirement of Eq.

Deriving PartialEq for your struct with an f64 field will use == on the field and in turn violate reflexivity for values containing NaN. This is the conceptual reason why the compiler will complain if you try to derive Eq. If you need Eq, one reasonably approach might be in disallowing NaN values in your field by using ordered_float::NotNan<f64>,