# \`checked\_sub\` and \`saturating\_sub\` for NonZero unsigned integers

**URL:** <https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898>\
**Category:** libs\
**Created:** [November 21, 2023, 2:49pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898 "2023-11-21T14:49:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wainwrightmark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wainwrightmark/32/11524_2.png) [@wainwrightmark](https://internals.rust-lang.org/u/wainwrightmark)\
**Post date:** [November 21, 2023, 2:49pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/1 "2023-11-21T14:49:18Z")

</div>

I just noticed that [`NonZeroU8`](https://doc.rust-lang.org/std/num/struct.NonZeroU8.html) and friends don't have a subtraction method. I felt like they ought to have `checked_sub` as they have `checked_add`, `checked_mul`, and `checked_pow`.

[Looking into it](https://github.com/rust-lang/rust/pull/82703), it seems like the reason `checked_sub` wasn't added was because it doesn't distinguish between a `None` that is returned because of overflow and a `None` that is returned because `0` is not a valid `NonZeroU8`. This seems like a perfectly reasonable way to constrain the effect of that PR but it is annoying to have to write something like `my_u8.get().checked_sub(42).and_then(|x| NonZeroUsize::new(x))` every time.

`saturating_sub` seems to have been left off for the same reason.

I'm thinking of making a RFC for this. Does anyone have any thoughts?

I think the method signatures for `NonZeroU8` should be

```rust

pub const fn checked_sub(self, other: u8) -> Option<NonZeroU8>

pub const fn saturating_sub(self, other: u8) -> NonZeroU8

```

Obviously I would also add the equivalents for all `NonZeroU*` types.

I don't think the `Sub` trait should be implemented as the `Add`, `Mul`, and `Pow` traits aren't.

I could add the equivalent `Div` and `Rem` methods at the same time but I think they are very odd. Returning an `Option<NonZeroU*>` from one of these seems weird. My instinct is that if they existed, they would more frequently be used accidentally than legitimately. Happy to be corrected about this.

Likewise, I'm not super interested in adding these for the signed nonzero types as those don't even have addition or multiplication yet.

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [November 21, 2023, 3:04pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/2 "2023-11-21T15:04:33Z")

</div>

Shouldn't `saturating_sub` always succeed, but return 1 in the event of underflow?

---

<div class="post-metadata">

**Author:** ![wainwrightmark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wainwrightmark/32/11524_2.png) [@wainwrightmark](https://internals.rust-lang.org/u/wainwrightmark)\
**Post date:** [November 21, 2023, 3:22pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/3 "2023-11-21T15:22:32Z")

</div>

Yes. Brain error on my part. Corrected.

---

<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:** [November 21, 2023, 4:28pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/4 "2023-11-21T16:28:17Z")

</div>

> [@wainwrightmark](#):
>
> I just noticed that [`NonZeroU8`](https://doc.rust-lang.org/std/num/struct.NonZeroU8.html) and friends don't have a subtraction method. I felt like they ought to have `checked_sub` as they have `checked_add`, `checked_mul`, and `checked_pow`.

This was recently discussed on URLO:

> **[Why there is no \`saturating\_sub()\` method on \`NonZeroU\[SIZE\]\` types](https://users.rust-lang.org/t/why-there-is-no-saturating-sub-method-on-nonzerou-size-types/102423/22?u=scottmcm)**
>
> Well, the original purpose of NonZeroU32 is to be "u32 but with a niche at zero". It didn't even have any of these math-related functions back when it was stabilized (see https://doc.rust-lang.org/1.35.0/std/num/struct.NonZeroU32.html for...

I liked @CAD97 's summary:

> I personally agree that `{saturating|checked}_sub`, if provided, should have the definition in integer ring 1..2N. But I fail to see a meaningful benefit in breaking the current dual applicability of `NonZeroUnn` that allows it to be a direct substitution for `uNN` in a fearless compiler error driven refactor.

---

<div class="post-metadata">

**Author:** ![wainwrightmark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wainwrightmark/32/11524_2.png) [@wainwrightmark](https://internals.rust-lang.org/u/wainwrightmark)\
**Post date:** [November 22, 2023, 6:58am UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/5 "2023-11-22T06:58:37Z")

</div>

Aha. I hadn't seen that post. That makes a lot of sense. If I have code that uses `U8` and change the type to `NonZeroU8` (or vice versa) it's a very nice property that any code that still compiles will continue doing the same thing.

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [November 22, 2023, 9:05am UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/6 "2023-11-22T09:05:17Z")

</div>

What about adding the functionality under different names?

---

<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:** [November 22, 2023, 1:30pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/7 "2023-11-22T13:30:36Z")

</div>

> [@wainwrightmark](#):
>
> If I have code that uses `U8` and change the type to `NonZeroU8` (or vice versa) it's a very nice property that any code that still compiles will continue doing the same thing.

This is already violated if you use `MIN`: `NonZeroU8::MIN` is 1, but `u8::MIN` is 0.

> [@CAD97](#):
>
> I personally agree that `{saturating|checked}_sub`, if provided, should have the definition in integer ring 1..2N.

Nitpick: that is not a ring.

I think `checked_sub` should be rather uncontroversial. `saturating_sub` returning 1 might be more surprising.

---

<div class="post-metadata">

**Author:** ![davidsk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davidsk/32/11544_2.png) [@davidsk](https://internals.rust-lang.org/u/davidsk)\
**Post date:** [November 25, 2023, 10:45pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/8 "2023-11-25T22:45:27Z")

</div>

> [@tczajka](#):
>
> `saturating_sub` returning 1 might be more surprising

A solution might be not naming them `saturating_sub` then when you refactor to `NonZeroXXX` while having `saturating_sub` calls you would get compiler errors. Those could be customized to hint towards the slightly differently named none zero equivalents.

You could call it `saturating_nonzero_sub`.

Another note, [Saturating in std::num - Rust](https://doc.rust-lang.org/std/num/struct.Saturating.html) also misses NonZeroXXX types. Might be best to keep them out from there, that seems better to me then introducing a `SaturatingNonZero` struct.

---

<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:** [February 23, 2024, 10:45pm UTC](https://internals.rust-lang.org/t/checked-sub-and-saturating-sub-for-nonzero-unsigned-integers/19898/9 "2024-02-23T22:45:38Z")

</div>

Closed on February 23, 2024, 10:45pm UTC

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