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

[第一章] 零成本抽象中2行代码编译报错 #308

Closed caozhengyx closed 3 years ago

caozhengyx commented 3 years ago

页码与行数


文本或排版错误

暂无


代码错误

5.times(|| println!("Hello Rust"));  // 此代码编译报错
2.days().from_now();  //此代码编译报错

Rust版本

$ rustc -V
rustc 1.46.0 (04488afe3 2020-08-24)

错误信息

cargo build
   Compiling guess v0.1.0 (D:\data\code\rust\guess)
error[E0599]: no method named `times` found for type `{integer}` in the current scope
 --> src\main.rs:2:7
  |
2 |     5.times(|| println!("Hello Rust"));
  |       ^^^^^ method not found in `{integer}`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `guess`.
   Compiling guess v0.1.0 (D:\data\code\rust\guess)
error[E0599]: no method named `days` found for type `{integer}` in the current scope
 --> src\main.rs:2:7
  |
2 |     2.days().from_now();
  |       ^^^^ method not found in `{integer}`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `guess`.

To learn more, run the command again with --verbose.
ZhangHanDong commented 3 years ago

@CaoZhenggg 这个代码只是演示、表意的作用的,本来就不能执行。感谢反馈。第二版我会注意加上标识。

caozhengyx commented 3 years ago

不建议用这个演示代码,演示代码应当是可以执行的,即便注明是演示代码也会让人困惑。

ZhangHanDong commented 3 years ago

@CaoZhenggg 有道理。