Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types 何时将数字文字指定给默认类型?_Types_Rust - Fatal编程技术网

Types 何时将数字文字指定给默认类型?

Types 何时将数字文字指定给默认类型?,types,rust,Types,Rust,我在玩一些代码,并做了以下观察: let x = 1; let () = x; error: mismatched types [E0308] note: expected type `_` note: found type `()` 这显然失败了,但我希望错误表明预期的类型是i32,而不是\uu。我发现未指定类型的浮点文本也会发生同样的情况,例如1.0 为什么会这样?该类型不应该被称为默认类型吗 更新:从Rust 1.12开始,错误消息的信息量更大: expected integ

我在玩一些代码,并做了以下观察:

let x = 1;
let () = x;

error: mismatched types [E0308]
note:  expected type `_`
note:     found type `()`
这显然失败了,但我希望错误表明预期的类型是
i32
,而不是
\uu
。我发现未指定类型的浮点文本也会发生同样的情况,例如
1.0

为什么会这样?该类型不应该被称为默认类型吗

更新:从Rust 1.12开始,错误消息的信息量更大:

expected integral variable, found ()

= note: expected type `{integer}`
= note:    found type `()`

Rust不仅从初始化中进行类型推断,而且从每个用法中进行类型推断。因此,它的类型检查器必须查看变量的每个用法,以确定它是什么类型,并且需要在执行过程中推断和检查类型


这意味着
let()=x是同一过程的一部分。它是
x
的用法,因此必须进行检查,以确定
x
可能是什么具体类型。在编译器仍在试图推断
x
的类型的同时,发现没有可能的类型可以匹配
()
,因此没有选择默认值,因为默认值只有在编译器查看了
x
的所有用法后才使用,而没有找到任何结果。

我认为失败太明显了。typechecker并没有深入到实际插入默认值的程度,因为这是不可能的。我只见过错误消息中的默认值,当时它是一个由闭包和泛型方法组成的严重混乱