let x: i32 = 4;
let y: i16 = 4;
println!("{}", x == y);
When compiling the snippet above, the compiler prints the following error:
error[E0308]: mismatched types
--> src/main.rs:5:25
|
5 | println!("{}", x == y);
| ^ expected i32, found i16
It seems that PartialEq
is not implemented for different types of integers. The same happens between f32
and f64
, and for PartialOrd
as well. Is there a reason for that? Is it intended to be implemented in future versions of Rust?
There are many integral types, in Rust:
i8
, i16
, i32
, i64
and i128
,u8
, u16
, u32
, u64
and u128
,isize
,usize
.In some cases, mixed arithmetic or comparisons would have an obvious implementation as a lossless conversion is possible in one direction:
i<x>
can always be converted to i<y>
if x < y
,u<x>
can always be converted to u<y>
if x < y
,u<x>
can always be converted to i<y>
if x < y
.Some conversions, however, are not obvious or not portable:
i<x>
cannot be converted to u<y>
no matter what the respective values of x
and y
are,isize
and usize
have a platform specific size anywhere, as small as 16 bits but as large as 64 bits.Therefore, since Rust is not keen on overflows or underflows, it is unlikely that arbitrary mixed arithmetic or comparisons will ever be implemented.
A restricted subset could be implemented, but then two questions are raised: