相关疑难解决方法(0)

如何在Rust中打印变量的类型?

我有以下内容:

let mut my_number = 32.90;
Run Code Online (Sandbox Code Playgroud)

我该如何打印my_number

使用typetype_of没有奏效.还有其他方法可以打印数字的类型吗?

types rust

204
推荐指数
11
解决办法
6万
查看次数

使用 FlatMap 迭代器时,为什么我会收到错误 FromIterator<&{integer}> is not implementation for Vec<i32> ?

考虑这个片段:

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}

rust

9
推荐指数
1
解决办法
3918
查看次数

何时将数字文字分配给默认类型?

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

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)

types rust

3
推荐指数
1
解决办法
127
查看次数

什么是`Vec :: new()`的默认类型?

我是Rust的新手.我知道Rust会在编译时预测绑定的类型.下面的代码编译并运行.

fn main() {
    let mut numbers = Vec::new();
    numbers.push(1);
}
Run Code Online (Sandbox Code Playgroud)

numbers矢量的默认类型是什么?

vector rust

1
推荐指数
2
解决办法
751
查看次数

标签 统计

rust ×4

types ×2

vector ×1