Why does char::to_digit return Option of u32 and not u8

If I understand correctly, the function converts a single digit in a base <= 36 to ist value. That means that the maximum number it can return is 35, which means a u8 is more than enough to hold it. So why is u32 used instead?


I'd say historical oversight. If you'd notice, the radix is also passed in as a u32.

Yeah. Let's do a const generics version that statically verifies the radix isn't too high.

u8 was considered (at least in passing).

1 Like

u32 is used fairly often in places as the "it's fine" type -- you'll notice that count_ones, for example, also returns it, even though no integer type in Rust can have anywhere near that many bits. And checked_shl on u8 takes u32, even though that's also completely overkill.


Probably related to the fact that apparently pre-1.0 used to have uint rather than u32 and a long tradition of the 32-bit integer type being called just "int" in other languages – even in C int has de facto been 32 bits on non-obscure architectures for decades and will likely stay that way forever because that's what everybody assumes.