How do I fix “wrong number of type arguments” while trying to implement a method?

你。 提交于 2019-11-30 06:04:14

问题


I'm trying to implement a method:

struct Point<T> {
    x: T,
    y: T,
}

struct Line<T> {
    start: Point<T>,
    end: Point<T>,
}

impl Line {
    fn length(&self) -> f64 {
        let dx: f64 = self.start.x - self.end.x;
        let dy: f64 = self.start.y - self.end.y;
        (dx * dx + dy * dy).sqrt()
    }
}

fn main() {
    let point_start: Point<f64> = Point { x: 1.4, y: 1.24 };
    let point_end: Point<f64> = Point { x: 20.4, y: 30.64 };

    let line_a: Line<f64> = Line {
        start: point_start,
        end: point_end,
    };
    println!("length of line_a = {}", line_a.length());
}

I'm getting this error:

error[E0243]: wrong number of type arguments: expected 1, found 0
  --> src/main.rs:11:6
   |
11 | impl Line {
   |      ^^^^ expected 1 type argument

What is causing this problem?


回答1:


You need to add a type parameter to the impl:

impl Line<f64> {
    fn length(&self) -> f64 {
        let dx: f64 = self.start.x - self.end.x;
        let dy: f64 = self.start.y - self.end.y;
        (dx * dx + dy * dy).sqrt()
    }
}


来源:https://stackoverflow.com/questions/35105788/how-do-i-fix-wrong-number-of-type-arguments-while-trying-to-implement-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!