ZhangHanDong / tao-of-rust-codes

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

第三章 代码清单 3-22 代码错误 #195

Closed huanyusun closed 5 years ago

huanyusun commented 5 years ago

页码与行数

代码错误

trait Add<RHS, Output> {
    fn add(self, rhs: RHS) -> Output;  //此处方法名应该为 my_add
}

impl Add<i32, i32> for i32 {
    fn my_add(self, rhs: i32) -> i32 {
        self + rhs
    }
}

impl Add<u32, i32> for u32 {
    fn my_add(self, rhs: u32) -> i32 {
        (self + rhs) as i32
    }
}

fn main() {
    let (a, b, c, d) = (1i32, 2i32, 3u32, 4u32);
    let x: i32 = a.my_add(b);
    let y: i32 = c.my_add(d);
    assert_eq!(x, 3i32);
    assert_eq!(y, 7i32);
}

Rust版本

$ rustc -V
rustc 1.34.0 (91856ed52 2019-04-10)

错误信息

error[E0407]: method `my_add` is not a member of trait `Add`
 --> src/main.rs:6:5
  |
6 | /     fn my_add(self, rhs: i32) -> i32 {
7 | |         self + rhs
8 | |     }
  | |_____^ not a member of trait `Add`

error[E0407]: method `my_add` is not a member of trait `Add`
  --> src/main.rs:12:5
   |
12 | /     fn my_add(self, rhs: u32) -> i32 {
13 | |         (self + rhs) as i32
14 | |     }
   | |_____^ not a member of trait `Add`

error[E0046]: not all trait items implemented, missing: `add`
 --> src/main.rs:5:1
  |
2 |     fn add(self, rhs: RHS) -> Output;
  |     --------------------------------- `add` from trait
...
5 | impl Add<i32, i32> for i32 {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `add` in implementation

error[E0046]: not all trait items implemented, missing: `add`
  --> src/main.rs:11:1
   |
2  |     fn add(self, rhs: RHS) -> Output;
   |     --------------------------------- `add` from trait
...
11 | impl Add<u32, i32> for u32 {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `add` in implementation

error: aborting due to 4 previous errors
ZhangHanDong commented 5 years ago

@huanyusun 感谢反馈,这个已经勘误过了。可以关注issues里closed列表,是曾经的勘误。