ZhangHanDong / tao-of-rust-codes

《Rust编程之道》随书源码
https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/
MIT License
1.18k stars 170 forks source link

[第5章] 5.5.4 代码清单5-42中trait对象在2021版本中需要在trait名称前加上dyn #336

Closed m104ngc4594 closed 1 year ago

m104ngc4594 commented 1 year ago

页码与行数


文本或排版错误

暂无


代码错误

let obj = box_bar as Box<Foo>;  // 此处应该是let obj = box_bar as Box<dyn Foo>;

Rust版本

$ rustc -V
rustc 1.67.0 (fc594f156 2023-01-24)

错误信息

Compiling playground v0.0.1 (/playground)
error[[E0782]](https://doc.rust-lang.org/stable/error-index.html#E0782): trait objects must include the `dyn` keyword
 --> src/main.rs:9:30
  |
9 |     let obj = box_bar as Box<Foo>;
  |                              ^^^
  |
help: add `dyn` keyword before this trait
  |
9 |     let obj = box_bar as Box<dyn Foo>;
  |                              +++

For more information about this error, try `rustc --explain E0782`.
error: could not compile `playground` due to previous error
m104ngc4594 commented 1 year ago

代码清单5-43中第7行也是同样的问题,在2021版本中需要在trait名称前加上dyn:

fn foo<'a>(s: &'a [u32]) -> Box<Foo<'a>> { // 此处应该是 fn foo<'a>(s: &'a [u32]) -> Box<dyn Foo<'a> + 'a> {