如何在尝试实现方法时修复 "wrong number of type arguments"?
How do I fix "wrong number of type arguments" while 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());
}
我收到这个错误:
error[E0243]: wrong number of type arguments: expected 1, found 0
--> src/main.rs:11:6
|
11 | impl Line {
| ^^^^ expected 1 type argument
是什么导致了这个问题?
您需要向 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()
}
}
我正在尝试实现一个方法:
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());
}
我收到这个错误:
error[E0243]: wrong number of type arguments: expected 1, found 0
--> src/main.rs:11:6
|
11 | impl Line {
| ^^^^ expected 1 type argument
是什么导致了这个问题?
您需要向 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()
}
}