0

In Rust by Example's Types chapter, the following example is given for initializing numeric literals:

fn main() {
    // Unsuffixed literals, their types depend on how they are used
    let i = 1;
    let f = 1.0;
}

I am confused by why "their types depend on how they are used". The Book states that "integer types default to i32" and that floating-point numbers default to f64. So shouldn't the types of i and f already be decided at initialization to be i32 and f64, respectively? Why would their types depend on anything else?

Telescope
  • 2,068
  • 1
  • 5
  • 22
  • 1
    TL;DR: the types will default to `i32` and `f64` *only if* the compiler is unable to infer a more specific type from context. – Joe Clay Jan 24 '23 at 16:56

0 Answers0