Subranges of usize

There are mentions in the documentation of "subranges of usize" being used as array indices. What is that?

Use case is that I have a lot of little arrays of size 3, and I want an Axis type limited to 0 to 2. Is there some way to declare a type which is limited to a subrange and can be used as an array index?

I mean, you could certainly define an Axis struct, limit its constructors to not allow values other than 0, 1, or 2 and manually implement Index for your container using the struct.

But as for what the documentation you're talking about means, I don't know. Where did you find it?

I'd guess that the documentation refers to the following:

let arr: [u8; 3] = [1, 2, 3];
let slice: &[u8] = arr[0..1];

i.e. to that fact that one can extract a subslice from array, and not only a single value.

Oh. OK. I hoped Pascal/Modula/Ada type subranges had been installed. Checking is when a value moves into the type; subscript checking can often be moved to compile time. Oh well.

I did some basic grepping in rust-lang/rust, and the only mentions of "ranges of usize" are in diagonstic messages (Rust Playground):

fn main() {
    let _ = [][()];
    let _ = ""[()];
}
   Compiling playground v0.0.1 (/playground)
error[E0277]: the type `[_]` cannot be indexed by `()`
 --> src/main.rs:2:13
  |
2 |     let _ = [][()];
  |             ^^^^^^ slice indices are of type `usize` or ranges of `usize`
  |
  = help: the trait `SliceIndex<[_]>` is not implemented for `()`
  = note: required because of the requirements on the impl of `Index<()>` for `[_]`

error[E0277]: the type `str` cannot be indexed by `()`
 --> src/main.rs:3:13
  |
3 |     let _ = ""[()];
  |             ^^^^^^ string indices are ranges of `usize`
  |
  = help: the trait `SliceIndex<str>` is not implemented for `()`
  = note: required because of the requirements on the impl of `Index<()>` for `str`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to 2 previous errors

Perhaps "subranges of usize" are mentioned in one of the books.

I think the term subranges is used because it means smaller ranges than 0..usize::MAX, which is the range entirely covering all usize values. I guess it's just a confusing wording:

  • In "subranges of usize", "usize" means the range from 0..usize::MAX.
  • In "ranges of usize", "usize" means the type usize.

(But that's just my guess.)