Document integer types


#1

I heard from several sources that Rust does not have a default integer type, that is neither int nor uint are supposed to be used everywhere, but rather only for indexing.

First of all: Is this true?

If so, I think this should be reflected in the documentation, and also important, it should be reflected in the code examples and tests, where usually just uint is chosen, when for example pushing things into a vector or constructing a tree, or similar. As an example, see the Vec docs (as of 2014-08-12):

let mut vec = Vec::new();
vec.push(1i);
vec.push(2i);

assert_eq!(vec.len(), 2);
assert_eq!(vec[0], 1);

assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.len(), 1);

*vec.get_mut(0) = 7i;
assert_eq!(vec[0], 7);

vec.push_all([1, 2, 3]);

for x in vec.iter() {
    println!("{}", x);
}
assert_eq!(vec, vec![7i, 1, 2, 3]);

#2

Curiously enough, my impression is that indexes/sizes/differences of the indexes do represent the absolute majority of integers in common non-numeric code. And for specialized numeric code you don’t want “default” integer types or index types, but quite definite types, like i32.

But the documentation problem exists, indeed. The recent example of the confusion can be found in benchmark here

I would blame tutorials/examples too. They overuse index types as numerics (and vice versa in C++) without regards to any semantics. Why? Just because they are simple - int, Vec<int>, what can be simpler and more familiar? Perfect types for toy 2-5-10 line examples. And then you have to relearn to use size_t for indexes in C++ and i32 for numeric calculations in Rust.


#3

https://github.com/rust-lang/rust/issues/15526 is relevant here.