# Int::{min,max} in const fn

**URL:** <https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232>\
**Category:** language design\
**Created:** [July 15, 2025, 3:10pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232 "2025-07-15T15:10:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alice](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alice/32/5044_2.png) [@alice](https://internals.rust-lang.org/u/alice)\
**Post date:** [July 15, 2025, 3:10pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/1 "2025-07-15T15:10:24Z")

</div>

Right now, if you want to compute the maximum of two numbers in const context, you have to do an `if a < b { b } else { a }` manually. This is because `usize::max()` evaluates to `<usize as Ord>::max` which is a trait method and hence can't be const.

Could we add inherent methods to the integer types to work around this?

```rust
impl usize {
    pub const fn min(self, rhs: Self) -> Self {
        if self > rhs { rhs } else { self }
    }
    pub const fn max(self, rhs: Self) -> Self {
        if self < rhs { rhs } else { self }
    }
}

```

This way, whenever anyone writes `usize::max(a,b)` it will evaluate to this method instead, which is const. It shouldn't be a breaking change since this does not change the behavior of anyone calling the trait method.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [July 15, 2025, 3:18pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/2 "2025-07-15T15:18:56Z")

</div>

We could, but I do expect that we'll make `Ord` a const trait soon.

---

<div class="post-metadata">

**Author:** ![VitWW](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vitww/32/10822_2.png) [@VitWW](https://internals.rust-lang.org/u/VitWW)\
**Post date:** [July 15, 2025, 4:10pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/3 "2025-07-15T16:10:55Z")

</div>

Maybe is it reasonable to add 2 new methods `const_max` and `const_min` to `impl usize` (and other integer types)?

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tczajka/32/8923_2.png) [@tczajka](https://internals.rust-lang.org/u/tczajka)\
**Post date:** [July 15, 2025, 4:23pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/4 "2025-07-15T16:23:59Z")

</div>

A lot of trait methods could be useful in const context. Since it's supposed to be a temporary workaround for a missing language feature, it's better to create a crate, say `const_trait_workarounds`, and later once the feature lands users can drop the dependency. Putting it in `std` would mean the deprecated workaround stays around forever.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [July 15, 2025, 6:20pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/5 "2025-07-15T18:20:48Z")

</div>

Are const traits actually nearing stabilization? I know there's been a ton of activity on that front but haven't been following it too closely.

---

<div class="post-metadata">

**Author:** ![alice](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alice/32/5044_2.png) [@alice](https://internals.rust-lang.org/u/alice)\
**Post date:** [July 16, 2025, 9:44am UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/6 "2025-07-16T09:44:26Z")

</div>

Part of the reason I suggest it is that const traits did not seem near stabilization _to me_. But I have not followed it that closely. But I do not think it's a particularly big technical debt to have a `max` method on integers. We already have them for f32/f64.

@VitWW I would not call them `const_max`/`const_min`. Just call them `min`/`max` and let them shadow the trait method.

---

<div class="post-metadata">

**Author:** ![JarredAllen](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JarredAllen](https://internals.rust-lang.org/u/JarredAllen)\
**Post date:** [July 16, 2025, 7:32pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/7 "2025-07-16T19:32:57Z")

</div>

It looks like there's tracking issues for making specific traits const-callable for specific std-lib types, which would be enough for this and I think will land well before support for const traits on arbitrary library traits/types. Though admittedly I haven't been following the most closely with that effort

---

<div class="post-metadata">

**Author:** ![conqp](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/conqp/32/12462_2.png) [@conqp](https://internals.rust-lang.org/u/conqp)\
**Post date:** [July 16, 2025, 10:25pm UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/8 "2025-07-16T22:25:24Z")

</div>

In the meantime, you can use [`const_utils`](https://docs.rs/const-utils/latest/const_utils/usize/fn.min.html).

---

<div class="post-metadata">

**Author:** ![theemathas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/theemathas/32/11817_2.png) [@theemathas](https://internals.rust-lang.org/u/theemathas)\
**Post date:** [July 17, 2025, 4:23am UTC](https://internals.rust-lang.org/t/int-min-max-in-const-fn/23232/9 "2025-07-17T04:23:49Z")

</div>

The [`konst`](https://docs.rs/konst/latest/konst/) crate has more stuff in it.
