Related to this is the concept that indexes shouldn't be "raw" integers at all - if Rust had value restricted types, then the allowed type for indexing would not be usize
, but would instead be a value-restricted type whose permitted range was exactly the same as the indexing range. This begins to take you in the direction of dependent types, though.
The extreme is not having bitwidth specifiers for integers at all; you'd specify an integer as having a range (e.g. Int<0..=255
>), and the compiler would determine signedness, bit width needed etc based on this range (so that would be a u8
in today's Rust, while Int<0..=254>
would be represented as a u8
, but 255
would not be a possible value. Int<-8..=16>
would be signed, and at least 5 bits large).