12

It is surprisingly hard to find this in the docs. This might even be a two part question:

  1. Are {integer} and {float} some sort of language alias for a specific primitive type?

  2. What does it mean for a type name to be enclosed in curly braces in a compilation/syntax error message?

Example:

error: no method named pow found for type {integer} in the current scope

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    Related question: [When are numeric literals assigned to default types?](http://stackoverflow.com/questions/39595782/when-are-numeric-literals-assigned-to-default-types). – ljedrz Feb 02 '17 at 14:09

2 Answers2

11

{integer} in error messages is a placeholder for any of the integer types ({i,u}{8,16,32,64,128}). (Source)

Integer literals in Rust are type inferred based on their usage. For example, in the following code, the type of 123 is u8 in the first instance and u64 in the second:

let a: u8 = 123;
let a: u64 = 123;

{integer} is used to represent any integer type in error messages when the compiler hasn't figured out a concrete type of the value.

Dogbert
  • 212,659
  • 41
  • 396
  • 397
11

{integer} is an integral value whose concrete type was not specified and has not been inferred by the compiler yet; the following code:

fn main() {
    let x = 1;
    let () = x;
}

Will result in the following error:

error[E0308]: mismatched types

 --> <anon>:3:9
  |
3 |     let () = x;
  |         ^^ expected integral variable, found ()
  |
  = note: expected type `{integer}`
  = note:    found type `()`

The same would happen with a floating number:

fn main() {
    let x = 1.0;
    let () = x;
}
error[E0308]: mismatched types
 --> <anon>:3:9
  |
3 |     let () = x;
  |         ^^ expected floating-point variable, found ()
  |
  = note: expected type `{float}`
  = note:    found type `()`

Because the compilation error caused by the invalid assignment let () = x is thrown before the type inference can happen.

In other words, until the compilation reaches the type inference stage where an integer or a float without a concrete type specified would be recognized (e.g. based on function application) or assigned the default type, i32 for integers and f64 for floats, compilation errors will refer to it as an {integer} or a {float}.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ljedrz
  • 20,316
  • 4
  • 69
  • 97
  • 1
    Note: the `let () = x` assignment is an useful way of determining the type of a variable: [related SO question](http://stackoverflow.com/questions/21747136/how-do-i-print-the-type-of-a-variable-in-rust). – ljedrz Feb 02 '17 at 08:11
  • That's essentially how I stumbled into this error. Was trying to exponentiate a value that I had just retrieved from a hash map, but had only been inserting numeric literals into that hashmap (working through Rust Koans), giving the compiler no information as to what specific numeric type to use, and then trying to coerce those values into arbitrary types to figure out just what type I was trying to code against. Another piece of the puzzle clicks into place. –  Feb 02 '17 at 08:21