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

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

> {integer}和{float}是特定基元类型的某种语言别名吗?
>将类型名称括在花括号中是什么意思
编译/语法错误消息?

例:

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

解决方法

{integer}是一个整数值,其具体类型未指定,尚未被编译器推断;以下代码
fn main() {
    let x = 1;
    let () = x;
}

将导致以下错误

error[E0308]: mismatched types

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

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

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 `()`

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

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

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效