简体   繁体   English

为什么我的特质定义与2015版本编译而不是与2018版本编译?

[英]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.rsrustc 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.

相关问题 Rust 2021 与 2018:特征 `std::marker::Send` 未实现 - 在 2021 版中执行此操作的正确方法是什么? - Rust 2021 vs. 2018: trait `std::marker::Send` is not implemented - what is the correct way to do this in 2021 edition? 为什么函数体在结构中编译,而不是在特征中编译? - Why does a function body compile in a struct, but not in a trait? 为什么此Rust 2018代码使用`cargo build`编译但不使用rustc编译? - Why does this Rust 2018 code compile with `cargo build` but not using rustc? 为什么我的特质需要一个终身参数? - Why does my trait need a lifetime parameter? 为什么在取消引用的特征对象或切片上调用方法会编译? - Why does calling a method on a dereferenced trait object or slice compile? 为什么我的Trait实现不匹配? - Why does my Trait implementation not match? 为什么`Self`在编译时需要一个恒定的大小来调用一个自由函数,但是等效的trait函数却没有? - Why does `Self` require a constant size at compile time for calling a free function but the equivalent trait function does not? 为什么 rustc compile 抱怨我的简单代码“特征 std::io::Read 没有为 Result 实现<File, anyhow::Error> ” - why rustc compile complain my simple code “the trait std::io::Read is not implemented for Result<File, anyhow::Error>” 为什么特征中的泛型方法需要调整特征对象的大小? - Why does a generic method inside a trait require trait object to be sized? 为什么 Rust 从特征到特征分配 const 不起作用? - Why does Rust assignment of const from trait to trait not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM