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

[第三章]代码清单3-36的trait对象在最新stable1.66报错,trait对象应改为&dyn Bar #334

Closed m104ngc4594 closed 1 year ago

m104ngc4594 commented 1 year ago

页码与行数


文本或排版错误

暂无


代码错误

2021 版本前是可以这么写,但现在是错误了。

 fn dynamic_dispatch(t: &Bar) {  // 此处应该是 fn dynamic_dispatch(t: &dyn Bar) {

Rust版本

[$ rustc -V
stable 1.66](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cf44aab55bc00936c1346c687fde14f9)

错误信息

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:13:29
   |
13 |     fn dynamic_dispatch(t: &Bar) {
   |                             ^^^
   |
help: add `dyn` keyword before this trait
   |
13 |     fn dynamic_dispatch(t: &dyn Bar) {
   |                             +++

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

感谢反馈。这个是 Rust 语言改进导致的编译问题,等第二版再升级吧。