我有以下内容:
let mut my_number = 32.90;
Run Code Online (Sandbox Code Playgroud)
我该如何打印my_number?
使用type并type_of没有奏效.还有其他方法可以打印数字的类型吗?
考虑这个片段:
fn main() {
let arr_of_arr = [[1, 2], [3, 4]];
let res = arr_of_arr
.iter()
.flat_map(|arr| arr.iter())
.collect::<Vec<i32>>();
}
Run Code Online (Sandbox Code Playgroud)
编译器错误是:
fn main() {
let arr_of_arr = [[1, 2], [3, 4]];
let res = arr_of_arr
.iter()
.flat_map(|arr| arr.iter())
.collect::<Vec<i32>>();
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码不能编译?
特别是,我无法理解错误消息:什么类型代表&{integer}?
我正在玩一些代码,并做了以下观察:
let x = 1;
let () = x;
error: mismatched types [E0308]
note: expected type `_`
note: found type `()`
Run Code Online (Sandbox Code Playgroud)
这显然失败了,但我期待错误说明预期的类型i32不是_.我发现同样的情况发生在一个未指定类型的浮动文字中,例如1.0.
为什么会这样?这种类型不应该被称为默认类型吗?
更新:从Rust 1.12开始,错误消息提供更多信息:
expected integral variable, found ()
= note: expected type `{integer}`
= note: found type `()`
Run Code Online (Sandbox Code Playgroud) 我是Rust的新手.我知道Rust会在编译时预测绑定的类型.下面的代码编译并运行.
fn main() {
let mut numbers = Vec::new();
numbers.push(1);
}
Run Code Online (Sandbox Code Playgroud)
numbers矢量的默认类型是什么?