How to compare variables of two types without type conversion or type casting using if statement

Is it possible to compare variables of two types without type conversion or type casting?

fn main() {
    let a: usize = 20;
    let b: f32 = 50.90;
    if a == b {
        println!("{} {}", a, b);
    } else {
        println!("{} {}", a, b);

This code outputs errors when comparing variable of different types, while other languages, like c, do not output any errors.

How do I solve this problem in Rust? If it is not possible, can anyone explain why?

For the question in title - it's possible to compare types A and B, if A: PartialEq<B>.
For the question in body: Rust does not perform implicit type casts, so, if there isn't an implementation of PartialEq (and there's none for usize vs f32), you must cast one of the variables explicitly.

Please see this.

If you are making tests for equality between floating point and integer types you almost certainly have a bugs in waiting in your programming career.

As such it is a very good thing that Rust won't let you do that without making you think about it.

Even testing for equality between floats in usually a bad idea.

so test of two different data type is bad habit while coding

i have one more doubt language like c and c++ allow compare variable of two type while rust don't (rust does not perform implicit type casts or due to any other reason) ,is this feature is a part of rust due to it focus on safety

You may have missed the point.

An old project manager of mine once found a new young team member using floats in some code he was writing. That manage said "If you think you need floating point to solve the problem then you don't understand the problem"

At the time we did not have hardware floating point on that system and floats were not needed anyway, our Coral compiler supported fixed point arithmetic.

Anyway I always thought that a corollary to that might be "If your really do need floating point to solve the problem, then you have a problem you won't understand". Which I came to think is more and more true over the years as I learned more about floating point.

If that makes little sense to you then do have a read of:

"What Every Computer Scientist Should Know About Floating-Point Arithmetic":
"What Every Programmer Should Know About Floating-Point Arithmetic"

From a purely mathematical perspective, forget about computers and IEE standards for a moment, it makes no sense to compare real numbers for equality. Consider: If I think of a real number between zero and one and ask you to guess it then the probability of you guessing the right number is zero. In as much as we can talk of a probability in that situation.

When it comes to comparing integers of different sizes and signedness then you have the possibility that values in one cannot even be represented by the possible values of the other. So comparing them makes no sense.

Now, if you the programmer know for sure that your u64 will never hold values bigger than your u32 by all means convert convert one to the other some how and compare them.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.