相关疑难解决方法(0)

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

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

  1. {integer}{float}特定原始类型的某种语言别名?

  2. 在编译/语法错误消息中用大括号括起类型名称是什么意思?

例:

错误:没有为当前范围中的pow类型命名的方法{integer}

compiler-errors rust

7
推荐指数
2
解决办法
1232
查看次数

无法从 `Iterator<Item=&String>` 构建类型为 `Vec<String>` 的值的迭代器收集问题

我在使用Iterator'sflat_map函数时遇到了困难,我不太确定如何理解和解决这个编译器错误。

我通过序列化两个结构将文件路径列表 flat_mapping 成两个字符串:

let body: Vec<String> = read_dir(query.to_string())
    .iter()
    .enumerate()
    .flat_map(|(i, path)| {
        let mut body: Vec<String> = Vec::with_capacity(2);

        let entry = Entry { i };
        body.push(serde_json::to_string(&entry).unwrap());

        let record = parse_into_record(path.to_string()).unwrap();
        body.push(serde_json::to_string(&record).unwrap());

        body.iter()
    })
    .collect();
Run Code Online (Sandbox Code Playgroud)
error[E0277]: a value of type `std::vec::Vec<std::string::String>` cannot be built from an iterator over elements of type `&std::string::String`
   --> src/main.rs:275:10
    |
275 |         .collect();
    |          ^^^^^^^ value of type `std::vec::Vec<std::string::String>` cannot be built from `std::iter::Iterator<Item=&std::string::String>`
    |
    = help: the trait `std::iter::FromIterator<&std::string::String>` …
Run Code Online (Sandbox Code Playgroud)

iterator rust

7
推荐指数
1
解决办法
7629
查看次数

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

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

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
查看次数

标签 统计

rust ×3

compiler-errors ×1

iterator ×1

types ×1