# Subscripts and sizes should be signed (redux)

**URL:** <https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296>\
**Category:** language design\
**Created:** [August 5, 2023, 12:44pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296 "2023-08-05T12:44:43Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [August 9, 2023, 11:23am UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/25 "2023-08-09T11:23:38Z")

</div>

> [@user16251](#):
>
> Unsigned types don't model natural numbers though; they model a quotient of the integers.

You mean modular arithmetic? Not really. That would be `Wrapping<u64>`, not `u64`.

> [@user16251](#):
>
> Sometimes it is useful to think of them limited precision 2-adic integers instead.

I don't think this makes sense. p-adic integers are an extension of rational numbers. For instance, in 2-adic integers, 1/3 = ...0101011. This doesn't have much to do with `u64`.

`u64` represents natural numbers, with a resource limit.

---

<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:** [August 9, 2023, 11:50am UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/26 "2023-08-09T11:50:14Z")

</div>

The distinction between `Wrapping<u64>` and `u64` is only salient in debug mode, but point taken. Anyway, for p-adics, I wrote that it's useful to think of `u64` that way sometimes and I stand by that.

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [August 9, 2023, 12:02pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/27 "2023-08-09T12:02:08Z")

</div>

> [@user16251](#):
>
> The distinction between `Wrapping<u64>` and `u64` is only salient in debug mode, but point taken.

There’s also the important pragmatic difference that the former actually reflects the programmer’s _intention_ (to do modular arithmetic), whereas in the latter case it doesn’t really matter what happens on overflow because any value returned is erroneous and the program almost certainly has just entered an invalid or at least unexpected state. Even though behavior on overflow is well-defined, it is not a salient part of the program’s semantics. That is, unless the programmer has intentionally turned overflow checks off also in debug mode to get ergonomic wrapping integers…

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [August 9, 2023, 12:10pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/28 "2023-08-09T12:10:19Z")

</div>

> [@user16251](#):
>
> But Rust also requires pointers to fit in a `usize` which might not work on some [experimental platforms](https://tratt.net/laurie/blog/2022/making_rust_a_better_fit_for_cheri_and_other_platforms.html).

Yeah, I think Rust developers recognize it as one of mistakes which got baked into the language. BTW it's not only about experimental architectures, the [Elbrus architecture](https://en.wikipedia.org/wiki/Elbrus_2000) has had "protected mode" (quite similar to the CHERI experiment) for quite some time and it even has helped to find a [bug](https://savannah.gnu.org/patch/?10081) in `tar`. Unfortunately, there is a lot of software which can not run properly in protected mode (and it also hurts performance a bit), so it's used quite rarely in practice.

---

<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:** [August 9, 2023, 12:26pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/29 "2023-08-09T12:26:55Z")

</div>

> [@user16251](#):
>
> One think you might want to do with an index `i` is compute `i - 1`. With both unsigned and signed `i` this can overflow but the value 0 is a lot more common than the value `isize::MIN` . It's usually easier to prove that the latter never happens (e.g. if `i` starts out nonnegative).

Usually if you do that with an index, then what you want is `i > 0`. If you aren't quite sure what the range is, and it so happens that `i == 0` sometimes and you do that by accident without realizing what you have done, a panic is _helpful_ to debug the problem.

Yes, in some scenarios you may want to do that deliberately and natural numbers aren't sufficient for some intermediate calculations that eventually come back out as natural numbers. That's not the common scenario.

In those rare cases where that's what you want, it's not a big problem to convert to whatever you need explicitly. I'd even say it has value for explicitly documenting what you're doing. Maybe you want `isize`. But maybe you want something else, such as `Wrapping<usize>`, or `i128`, or `Rational` etc.

A similar scenario is when you're solving 3rd degree polynomial equations in real numbers. You have to move to `Complex` numbers in your intermediate calculations. Well, so you convert. That doesn't require that you use complex numbers everywhere by default.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 9, 2023, 4:54pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/30 "2023-08-09T16:54:43Z")

</div>

> [@user16251](#):
>
> (Indices also can't be really large and the `isize` type actually captures that constraint so it's not as clear-cut as it seems.)

This is inaccurate. Slice indices can validly be as large as `usize::MAX` just fine. The `isize::MAX` limit is to the maximum size of any single allocation. Thus slices of zero-sized types may have a length greater than `isize::MAX` with no issues.

Furthermore, the actual upper limit on valid slice length is lower than `isize::MAX` when the element type is larger than a single byte, and on many architectures the actual limit to allocation size is multiple orders of (binary) magnitude below `isize::MAX`, so even byte slices can't validly be that long. (Try using an array type that big, and rustc will complain that the type is to big for the architecture.)

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [August 9, 2023, 7:00pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/31 "2023-08-09T19:00:43Z")

</div>

Isn't this whole topic kind of pointless? None of this can be changed now without breaking backward compatibility if I understand things correctly.

And even if an edition could be used for this (which I doubt, how would indexing a type from another crate compiled with a different edition work?), it would be a massive change that wouldn't be worth it.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [August 9, 2023, 8:12pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/32 "2023-08-09T20:12:38Z")

</div>

> [@tczajka](#):
>
> `u64` represents natural numbers, with a resource limit.

In math the result of subtraction of two natural numbers is an integer number. In Rust the result of subtraction of two natural numbers is a natural number or a program fail with big chance.

If you wanted to represent math abstraction in the language - you made it wrong.

> [@tczajka](#):
>
> In those rare cases where that's what you want, it's not a big problem to convert to whatever you need explicitly. I'd even say it has value for explicitly documenting what you're doing.

I can continue.

"RAII is a bad conception because it's not a big problem to delete whatever you need explicitly. I'd even say it has value for explicitly documenting what you're doing."

It is an absurd. Of course, I think, that RAII is very good conception, but using " it has value for explicitly documenting" you can proof any absurdistic sentence.

---

<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:** [August 9, 2023, 8:56pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/33 "2023-08-09T20:56:55Z")

</div>

> [@T4r4sB](#):
>
> In math the result of subtraction of two natural numbers is an integer number. In Rust the result of subtraction of two natural numbers is a natural number or a program fail with big chance.

No, there are different versions of each arithmetic operation, defined in different domains.

A more commonly seen example is the square root: you can define it only for x \>= 0 and leave sqrt(negative) undefined (and that's how `f32::sqrt` does it), or you can define it for negative numbers also, or you can define it for all complex numbers, or even for whole matrices of numbers.

Similarly, when dealing with arithmetic operations on natural numbers, even in pure math, [one commonly defines](https://people.math.ethz.ch/~halorenz/4students/LogikGT/Ch08.pdf) a subtraction operation that is only defined in the domain a \>= b since that is the inverse of natural number addition. Extending it to give you negative numbers only really is useful if you also extend the domain to make it work on all pairs of integers.

---

<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:** [August 9, 2023, 9:00pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/34 "2023-08-09T21:00:32Z")

</div>

> [@Vorpal](#):
>
> Isn't this whole topic kind of pointless? None of this can be changed now without breaking backward compatibility if I understand things correctly.

I think there is value in discussing whether decisions already made make sense. It's useful for how you advertise a feature: "it's a good feature" vs "we're stuck with it only for legacy reasons". You can also change documentation and give advice -- for instance, OP suggested advising users to use `isize` as the default integral type. One could also slowly phase out standard arrays and slices etc in favor of some alternative type. C++ has basically done this over time, replacing C-style arrays with `std::array`.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [August 9, 2023, 9:19pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/35 "2023-08-09T21:19:55Z")

</div>

> [@tczajka](#):
>
> Similarly, when dealing with arithmetic operations on natural numbers, even in pure math, [one commonly defines](https://people.math.ethz.ch/~halorenz/4students/LogikGT/Ch08.pdf) a subtraction operation that is only defined in the domain a \>= b

Ok, nice catch, I tell wrong. So math natural numbers is bad type for programming language because nobody expect that subtracting two numbers can fail the program.

---

<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:** [August 9, 2023, 9:32pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/36 "2023-08-09T21:32:45Z")

</div>

> [@T4r4sB](#):
>
> > [@tczajka](#):
> >
> > In those rare cases where that's what you want, it's not a big problem to convert to whatever you need explicitly. I'd even say it has value for explicitly documenting what you're doing.
> 
> [sarcasm snipped out]
> 
> It is an absurd. Of course, I think, that RAII is very good conception, but using " it has value for explicitly documenting" you can proof any absurdistic sentence.

You missed the "rare" part, and skipped the part where I said that the panic is desired and useful in most cases.

The difference between negative indices and RAII is that with RAII you _want_ to release the resources by default (if you use RAII, releasing the resources eventually is what you normally want), whereas for indexing you don't typically want the index to go negative by default.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [August 9, 2023, 10:35pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/37 "2023-08-09T22:35:27Z")

</div>

> [@tczajka](#):
>
> You missed the "rare" part, and skipped the part where I said that the panic is desired and useful in most cases.

What does mean "rare"? How ofter do you have a Rust' program crash because of signed numbers? Infinity times rarer. Panic is bad when logic really works good with negative numbers.

> [@tczajka](#):
>
> RAII you _want_ to release the resources by default

According to absurdistic logic: "without RAII you need to think about architecture very well, so non-RAII lang makes your program better".

> [@tczajka](#):
>
> whereas for indexing you don't typically want the index to go negative by default.

Whereas unsigned numbers are bad, because for arithmetic operations I dont typically want to have a program crash in simple arithmetic logic.

---

<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:** [August 10, 2023, 12:37am UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/38 "2023-08-10T00:37:35Z")

</div>

Is there any code out there that uses giant slices of zero-sized types?

---

<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:** [August 10, 2023, 12:53am UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/39 "2023-08-10T00:53:27Z")

</div>

This has come up before in this thread. Panicking is not necessarily what you want. With signed indices you could write something like `x.get(i - 1).unwrap_or_default()` when you need this operation. What makes you think this scenario is uncommon? Some people use `&` and `count_ones()` all the time; other people probably go years without using them. Some Rust programmers probably exclusively use iterators and never use index notation.

I agree that it's not a big problem because you could write `x.get(i.wrapping_sub(1)).unwrap_or_default()`. But it is a little annoying. For the kind of code I write small negative numbers come up a lot more than integers near `2^64`. Both of these are illegal indices most of the time and I'd rather my index type have the former rather than the latter.

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [August 10, 2023, 1:43am UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/40 "2023-08-10T01:43:41Z")

</div>

> [@Vorpal](#):
>
> Isn't this whole topic kind of pointless? None of this can be changed now without breaking backward compatibility if I understand things correctly.

There is a path forward though. A lot of the breakage is around type inference, we need this code to compile:

```rust
let a = [1, 2, 3];
let i = 1; // what is the type of `i`? (It needs to be `usize`)
a[i];

```

If there were a way to do

```rust
impl<T, I> ops::Index<I> for [T]
- where I: SliceIndex<[T]>,
+ where I: Into<SliceIndex<[T]>> ≋ default usize,
{ ... }

```

Then this discussion becomes more reasonable.

---

<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:** [August 10, 2023, 7:45am UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/41 "2023-08-10T07:45:48Z")

</div>

> [@user16251](#):
>
> Panicking is not necessarily what you want. With signed indices you could write something like `x.get(i - 1).unwrap_or_default()` when you need this operation. What makes you think this scenario is uncommon?
> 
> [...]
> 
> For the kind of code I write small negative numbers come up a lot more than integers near `2^64` .

It's obviously less common than the scenario where you don't want to use negative indexes in a `Vec` or a `str`. The question isn't "are negative indices more common than numbers near 2^64", the question is "are negative indices more commonly valid, or are they more commonly a bug".

For your example, I don't know your exact use case, but let's say you want to have a data structure that can be indexed infinitely to the left and to the right, with a signed index (say `isize`), and starts out with `Default` values everywhere, kind of like the tape in Turing machines.

What you can do in that scenario is implement this abstraction as a new type, `struct InfiniteTape`, using `VecDeque` internally, and implement `Index<isize>` and `IndexMut<isize>` on it. You will need **one or two** type conversions for this in your whole program, rather than having to repeat this logic in many places.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [August 10, 2023, 2:18pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/42 "2023-08-10T14:18:03Z")

</div>

> [@T4r4sB](#):
>
> There should be only one "default" integer which can be used in most cases and recommended everywhere, and other types only for cases where bit representation matters.

I don't buy this at all. I fact, I don't even buy the premise of the title of this thread.

Conceptually `array[-1]` is incoherent. That e.g. Python uses it for RTL indexing is a convenience, but doesn't change that base fact.

But even if it was coherent: Rust is stable. It will not change this, because moving from `usize` to `isize` would eg mean that if I created a Vec of length between `isize::MAX` and `usize::MAX`, that would suddenly break.

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/notriddle/32/14082_2.png) [@notriddle](https://internals.rust-lang.org/u/notriddle)\
**Post date:** [August 10, 2023, 2:31pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/43 "2023-08-10T14:31:50Z")

</div>

> [@T4r4sB](#):
>
> According to absurdistic logic: "without RAII you need to think about architecture very well, so non-RAII lang makes your program better".

This is arguing against a straw man. They’ve already pointed out that they think RAII is a good thing.

If the discussion is going to go anywhere productive, you need to try to understand other people’s point of view instead of arguing against exaggerated, “absurd” versions of it.

---

<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:** [August 10, 2023, 11:38pm UTC](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296/44 "2023-08-10T23:38:39Z")

</div>

I am not arguing that `x[-1]` should be legal. I am arguing that the type that represents indices should include the value `-1` and that is more useful than `usize::MAX`. I'm not sure what it is you think is "obvious." The `get` example is just one example. For another, you can't represent the difference between two array indices with `usize`. I want to do more with array indices than use them as arguments to `Index::index`.

[Previous page](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296.md?page=1)

[Next page](https://internals.rust-lang.org/t/subscripts-and-sizes-should-be-signed-redux/19296.md?page=3)
