I vote for option #1. I donât quite see how not having an int type would be a show stopper for people new to rust. For instance thereâs no float type, only f32 and f64.
In particular, in the âProsâ for option #2 I read âThis design encourages people to use 32-bit integers when they donât have a better idea in mind.â
Is that really a âproâ though? As you mention in option #4 unless youâre using bigints you probably canât ignore the width of the type. So Iâd say that might just be encouraging sloppy programming.
Maybe Iâm biased because Iâm mostly doing low level programming these days, but are there really cases where some of you write code in C, C++, rust or whatever and donât care about the width of the integer type? I donât have a concrete use case for the âGood enoughâ integer and what does âGood enoughâ even mean? If you tell the user (especially those coming from languages like python which use bigints for basic integer types) that the rust int is good enough youâre giving them a gun to shoot themselves in the foot.
Iâve made a quick unscientific survey of the C and C++ code lying on my hard drive (both mine and third party code). The only uses of ints I see are either:
- code assuming itâs at least a certain size (C garantees itâs at least 16bits, Iâve seen a bunch of âassert(sizeof(int) == 4â as well), but in this case you can easily just use an u16 or u32, theyâre probably just using int because it might look nicer or doesnât have to include stdint.h or other headers.
- iterating through an array (and thatâs arguably dangerous if you arenât making sure the size of the array fits the int)
- signaling errors in return values but thatâs not really a use case for rust and itâs still about assuming a minimal range for
int
When doing maths with loosely constrained ranges I usually end up using floating point, not integers (or bigints if theyâre available). And when I need to do arithmetics with integers Iâm very careful about not overflowing. The soon to be added debug checks for overflow would help with that though.
Perhaps less importantly, making int an alias for i32 would happen to match the C int type on x86 and amd64 but that wouldnât be true on all other architectures. If support for one such architecture (where the C int is not 32bit) is added at some point then existing broken FFI code that uses the rust int type to match the C int type would break. Again, not really a major concern at that point but I thought it was worth considering.
However Iâm in favour of adding some form of integer coercion to limit the use of casts, although maybe simply allowing slices/arrays to be indexed by any unsigned integer type would be enough? That seems a bit more conservative than allowing coercion in the general case. Implicit type conversions is one thing that I really donât like about C, although I suppose itâs not so bad if you only allow it to a bigger type.
But then as you mention that would make coercing to and from the isize type change from architecture to architecture and that sounds pretty nasty to me. If I understand correctly that would make this code build on amd64 but not on 32bit architectures:
fn foo(index: u64) -> T {
some_slice[index]
}
Iâm not really sure I like the sound of that.