【问题标题】:"Expecting a type here because of type ascription"“由于类型归属,这里期待一个类型”
【发布时间】:2018-06-08 04:47:15
【问题描述】:

我有一些代码试图将字符串列表读入结构值。使用下面的代码,我试图从POLIR::generate_config() 内部打印来自input_file 向量的行。我收到错误消息:

error: expected type, found `{`
 --> src/main.rs:5:27
  |
5 |         for line in args: {
  |                           ^ expecting a type here because of type ascription

我在这里做错了什么?

struct POLIR {}

impl POLIR {
    fn generate_config(&self, args: Vec<String>) {
        for line in args: {
            println!{"{}", line};
        }
    }
}

fn main() {
    //other program stuff
    let input_file = lines_from_file(input_file);

    let system = POLIR {};

    POLIR::generate_config(&system, input_file);
}

【问题讨论】:

  • 重新读取错误。 "did you mean to use ';' here?".
  • 我会指给您the book which has examples on using for...loops 供您查看。您的代码中有一个非常明显的语法错误。
  • 啊,我明白了。感谢您的帮助。 @SimonWhitehead
  • python 太多了?
  • Idiomatic Rust 对变量、方法、宏和字段使用 snake_case,对类型使用 UpperCamelCase。请改用Polir

标签: rust


【解决方案1】:

通过从POLIR::generate_config() 中删除冒号解决了这个错误:

fn generate_config(&self, args: Vec<String>) {
    for line in args {
        println!{"{}", line};
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多