# Comparisons between signed and unsigned integers?

**URL:** <https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029>\
**Category:** Uncategorized\
**Created:** [December 25, 2022, 12:34am UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029 "2022-12-25T00:34:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![user16251](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/user16251/32/10186_2.png) [@user16251](https://internals.rust-lang.org/u/user16251)\
**Post date:** [December 25, 2022, 12:34am UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/1 "2022-12-25T00:34:32Z")

</div>

Rust integer types now have member functions to add signed and unsigned types and check if the result is representable. What about comparisons between signed and unsigned types? These are easy to implement (e.g. `x < 0 || (x as u64) < y` safely compares signed `x` with unsigned `y`), so my question is more about syntax. Has this been proposed somewhere? Is there a reasonable solution that doesn't result in an explosion of methods?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 27, 2022, 5:47pm UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/2 "2022-12-27T17:47:15Z")

</div>

What I really want for this is for mixed-type comparison operators to just work. Because they only return `bool`, there's no fundamental problems with that like there is for implicit widening.

Unfortunately, it's currently blocked on smarter inference, because it'd be a breaking change right now.

---

<div class="post-metadata">

**Author:** ![tschuett](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tschuett/32/8380_2.png) [@tschuett](https://internals.rust-lang.org/u/tschuett)\
**Post date:** [December 29, 2022, 7:54pm UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/3 "2022-12-29T19:54:20Z")

</div>

C++ introduced safe comparison functions: [https://en.cppreference.com/w/cpp/utility/intcmp](https://en.cppreference.com/w/cpp/utility/intcmp)

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 30, 2022, 9:48pm UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/4 "2022-12-30T21:48:01Z")

</div>

> [@tschuett](#):
>
> C++ introduced

That's a very C++ solution -- add a new thing and just tell people not to use the C thing.

For their constraints that makes sense (especially since there's a defined semantic for it right now between types) but hopefully we can do better in Rust.

---

<div class="post-metadata">

**Author:** ![user16251](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/user16251/32/10186_2.png) [@user16251](https://internals.rust-lang.org/u/user16251)\
**Post date:** [December 31, 2022, 11:01pm UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/5 "2022-12-31T23:01:20Z")

</div>

This raises a question for me that might be more appropriate on URLO. Is it workable to define traits on pairs of types through tuples? E.g. for this application you could imagine a trait `Ord2` implemented for `(T, T)` if `T: Ord` but also for arbitrary pairs `(S, T)` if `S` and `T` implement `Into<i128>`. These overlap (which is too bad because we have something like 100 possible comparisons). Is this an anti-pattern?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 1, 2023, 6:52am UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/6 "2023-01-01T06:52:45Z")

</div>

> [@user16251](#):
>
> Is it workable to define traits on pairs of types through tuples?

`PartialOrd` already has a generic type, so we just need to implement all the `u64: PartialOrd<i64>` & such. If it weren't for the inference breakage, we could do it tomorrow.

The combinations is a bit of a pain, but they're already done with macros anyway, so it's not a big deal to generate more with macros. (Though it's be nice to have `U<N>` and just have a generic one. But that's a whole other conversation.)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 1, 2023, 6:53am UTC](https://internals.rust-lang.org/t/comparisons-between-signed-and-unsigned-integers/18029/7 "2023-04-01T06:53:18Z")

</div>

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