In https://doc.rust-lang.org/book/primitive-types.html#numeric-types, it said that in
let x = 42; // x has type i32
That means x
has the type i32
as default.
But in http://rustbyexample.com/cast/literals.html, it says that
Unsuffixed literal, their types depend on how they are used
I know I can't use i32
to index the vector, but the following code works:
fn main() {
let v = vec![1, 2, 3, 4, 5];
let j = 1; // j has default type i32? or it has type when it is first used?
// And what is the type of 1?
println!("{}", v[1]); // is 1 a usize?
println!("{}", v[j]);
}
So, what is the type of a literal integral value?