编译器错误消息中的{integer}或{float}是什么?

7 compiler-errors rust

很难在文档中找到它.这可能是一个两部分问题:

  1. {integer}{float}特定原始类型的某种语言别名?

  2. 在编译/语法错误消息中用大括号括起类型名称是什么意思?

例:

错误:没有为当前范围中的pow类型命名的方法{integer}

Dog*_*ert 10

{integer}错误消息是任何整数类型({i,u}{8,16,32,64,128})的占位符.(来源)

Rust中的整数文字是根据其用法推断的类型.例如,在下面的代码的类型,123u8在所述第一实例和u64所述第二:

let a: u8 = 123;
let a: u64 = 123;
Run Code Online (Sandbox Code Playgroud)

{integer} 用于表示编译器未找到值的具体类型时错误消息中的任何整数类型.


lje*_*drz 9

{integer}是一个整数值,其具体类型未指定,尚未由编译器推断; 以下代码:

fn main() {
    let x = 1;
    let () = x;
}
Run Code Online (Sandbox Code Playgroud)

将导致以下错误:

error[E0308]: mismatched types

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

浮点数也会发生同样的情况:

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

因为在let () = x类型推断发生之前抛出了由无效赋值引起的编译错误.

换句话说,直到编译到达类型推断阶段,其中将识别没有指定具体类型的整数或浮点数(例如,基于函数应用程序)或分配默认类型,i32对于整数和f64浮点数,编译错误将参考它作为一个{integer}或一个{float}.