Is StructuralEq implemented for values rather than for types?

โš“ Rust    ๐Ÿ“… 2026-01-02    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

The code below attempts to match an f32 value.

const NAN: f32 = f32::NAN;

match f32_value {
    // These constant patterns are allowed.
    0.0 => (),
    f32::INFINITY => (),

    // error: cannot use NaN in patterns
    // evaluates to `NaN`, which is not allowed in patterns
    f32::NAN => (),
    NAN => (),

    _ => (),
}

NaN is never equal to any value, so I can see why matching against NaN is disallowed. Thatโ€™s not my concern here; my question is what StructuralEq is and how it is implemented.

The page for StructuralEq says that it is a "Required trait for constants used in pattern matches."

Does that mean the constant 0.0 implements StructuralEq but the constant f32::NAN does not? Is StructuralEq implemented per value rather than per type?

I'm so confused because the values appearing in the code are all the same type: f32. How can one f32 value implement StructuralEq while another does not?

7 posts - 4 participants

Read full topic

๐Ÿท๏ธ Rust_feed