[英]Why does my trait definition compile with the 2015 edition but not with the 2018 edition?
I wrote this simple program: 我写了这个简单的程序:
trait Command<T> {
fn execute(&self, &mut T);
}
fn main() {
let x = 0;
}
I compiled this with rustc --edition=2018 main.rs
and get the error message: 我用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
Compiling via rustc --edition=2015 main.rs
or rustc main.rs
doesn't cause this error, although there are some warnings. 通过rustc --edition=2015 main.rs
编译rustc --edition=2015 main.rs
或rustc main.rs
不会导致此错误,尽管有一些警告。
What's the problem with this code? 这段代码有什么问题?
Anonymous trait parameters have been removed in 2018 edition: No more anonymous trait parameters . 2018年版中删除了匿名特征参数 : 不再有匿名特征参数 。
Add _:
before &mut T
if you want to ignore the parameter: 如果要忽略参数,请添加_:
before &mut T
:
trait Command<T> {
fn execute(&self, _: &mut T);
}
Compiling with rustc main.rs
works, because it defaults to --edition=2015
. 使用rustc main.rs
编译,因为它默认为--edition=2015
。
Indeed, if you put your main.rs
in a new Cargo project, then remove edition = "2018"
from Cargo.toml
, and run 实际上,如果你把你的main.rs
放在一个新的Cargo项目中,那么从Cargo.toml
删除edition = "2018"
,并运行
cargo fix --edition
then Cargo will add the missing _:
automatically. 然后Cargo将自动添加缺失的_:
See Transitioning an existing project to a new edition . 请参阅将现有项目转换为新版本 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.