Rust explicit type annotation vs integer literal suffix
As we know, there are two similar ways of explicit type hints in Rust:
let val: i16 = 777;
// and:
let val = 777i16;
My questions is: what are the benefits of both of them? (Let's only look at the case of literals, not runtime values). They seem identical to me.
I find using the literal annotation (777i16
) is often useful when creating an array or vector, and passing it to a function that accepts multiple types. For example
fn take_value<T>(input: Vec<T>) {
// ...
}
fn main() {
take_value(vec![0u16; 1024]);
}
Otherwise I prefer the explicit annotation for readability.