为什么我的特征定义与2015年版,但不能与2018版编译?

问题描述 投票:8回答:1

我写了这个简单的程序:

trait Command<T> {                                                                                                      
    fn execute(&self, &mut T);                                                                                          
}                                                                                                                       

fn main() {                                                                                                             
    let x = 0;                                                                                                          
}    

rustc --edition=2018 main.rs编译这一点,得到的错误信息:

error: expected one of `:` or `@`, found `)`
 --> main.rs:2:29
  |
2 |     fn execute(&self, &mut T);
  |                             ^ expected one of `:` or `@` here

通过rustc --edition=2015 main.rsrustc main.rs编译不会导致这个错误,虽然也有一些警告。

什么是与此代码的问题?

rust rust-2018
1个回答
9
投票

匿名特性参数已经在2018版被删除:No more anonymous trait parameters

_:前加&mut T如果你想忽略的参数:

trait Command<T> {
    fn execute(&self, _: &mut T);
}

rustc main.rs作品编译,因为它默认为--edition=2015


事实上,如果你把你的main.rs在新货的项目,然后从edition = "2018"删除Cargo.toml,并运行

cargo fix --edition

该货物将自动添加缺少的qazxsw POI。见qazxsw POI。

© www.soinside.com 2019 - 2024. All rights reserved.