微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

编译器错误 – 编译器错误消息中的{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}.

原文地址:https://www.jb51.cc/css/217718.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。