结构字段不可访问

Struct fields are not accessible

我试图访问结构的不同字段,但出现以下错误:

error[E0609]: no field `x` on type `Point`
 --> src/lib.rs:9:31
  |
8 |     fn distance<Point>(point1: &Point, point2: &Point) -> f64 {
  |                 ----- type parameter 'Point' declared here
9 |         let diffX = (*point1).x - (*point2).x;
  |                               ^

对于 x、y 和 z:

pub struct Point {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Point {
    fn distance<Point>(point1: &Point, point2: &Point) -> f64 {
        let diffX = (*point1).x - (*point2).x;
        let diffY = (*point1).y - (*point2).y;
        let diffZ = (*point1).z - (*point2).z;
        (diffX * diffX + diffY * diffY + diffZ * diffZ).sqrt()
    }
}

我做错了什么?

error[E0609]: no field `x` on type `Point`
 --> src/lib.rs:9:31
  |
8 |     fn distance<Point>(point1: &Point, point2: &Point) -> f64 {
  |                 ----- type parameter 'Point' declared here
9 |         let diffX = (*point1).x - (*point2).x;
  |                               ^

错误消息显示 Point 是第 8 行中声明的 type parameter,它隐藏了之前声明的原始 Point 类型。

因此解决方案是将此通用类型更改为另一个名称或将其删除,因为我在函数体中找不到它的任何用法。

pub struct Point {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Point {
    fn distance(point1: &Point, point2: &Point) -> f64 {
        let diffX = (*point1).x - (*point2).x;
        let diffY = (*point1).y - (*point2).y;
        let diffZ = (*point1).z - (*point2).z;
        (diffX * diffX + diffY * diffY + diffZ * diffZ).sqrt()
    }
}