Rust 如何在一个结构上实现ops::Mul,以便它可以与数值类型以及另一个结构一起工作?

Rust 如何在一个结构上实现ops::Mul,以便它可以与数值类型以及另一个结构一起工作?,rust,traits,Rust,Traits,我已经实现了一个Point3D结构: use std::ops; #[derive(Debug, PartialEq)] pub struct Point3D { pub x: f32, pub y: f32, pub z: f32, } impl ops::Add<&Point3D> for &Point3D { type Output = Point3D; fn add(self, rhs: &Point3D) -

我已经实现了一个
Point3D
结构:

use std::ops;
#[derive(Debug, PartialEq)]
pub struct Point3D {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl ops::Add<&Point3D> for &Point3D {
    type Output = Point3D;
    fn add(self, rhs: &Point3D) -> Point3D {
        Point3D {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

impl ops::Sub<&Point3D> for &Point3D {
    type Output = Point3D;
    fn sub(self, rhs: &Point3D) -> Point3D {
        Point3D {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

impl ops::Mul<&Point3D> for &Point3D {
    type Output = f32;
    fn mul(self, rhs: &Point3D) -> f32 {
        self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
    }
}

//Scalar impl of ops::Mul here

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn addition_point_3D() {
        let point1 = Point3D {
            x: 1.0,
            y: 2.0,
            z: 3.0,
        };
        let point2 = Point3D {
            x: 4.0,
            y: 5.0,
            z: 6.0,
        };
        let result = &point1 + &point2;
        assert_eq!(
            result,
            Point3D {
                x: 5.0,
                y: 7.0,
                z: 9.0
            },
            "Testing Addition with {:?} and {:?}",
            point1,
            point2
        );
    }

    #[test]
    fn subtraction_point_3D() {
        let point1 = Point3D {
            x: 1.0,
            y: 2.0,
            z: 3.0,
        };
        let point2 = Point3D {
            x: 4.0,
            y: 5.0,
            z: 6.0,
        };
        let result = &point1 - &point2;
        assert_eq!(
            result,
            Point3D {
                x: -3.0,
                y: -3.0,
                z: -3.0
            },
            "Testing Subtraction with {:?} and {:?}",
            point1,
            point2
        );
    }

    #[test]
    fn point3D_point3D_multiplication() {
        let point1 = Point3D {
            x: 1.0,
            y: 2.0,
            z: 3.0,
        };
        let point2 = Point3D {
            x: 4.0,
            y: 5.0,
            z: 6.0,
        };
        let result = &point1 * &point2;
        assert_eq!(
            result, 32.0,
            "Testing Multiplication with {:?} and {:?}",
            point1, point2
        );
    }

    /*
    #[test]
    fn point3D_scalar_multiplication() {
        let point1 = Point3D { x: 1.0, y: 2.0, z: 3.0};
        let scalar = 3.5;
        let result = &point1 * &scalar;
        assert_eq!(result, Point3D { x: 3.5, y: 7.0, z: 10.5 }, "Testing Multiplication with {:?} and {:?}", point1, scalar);
    }
    */
}
使用std::ops;
#[派生(调试,部分Q)]
发布结构点3D{
酒吧x:f32,
酒吧y:f32,
酒吧z:f32,
}
impl ops::为&Point3D添加{
类型输出=Point3D;
fn添加(自身,右侧:&Point3D)->Point3D{
Point3D{
x:self.x+rhs.x,
y:self.y+rhs.y,
z:self.z+rhs.z,
}
}
}
impl ops::用于&Point3D的Sub{
类型输出=Point3D;
fn sub(自身,右侧:&Point3D)->Point3D{
Point3D{
x:self.x-rhs.x,
y:self.y-rhs.y,
z:self.z-rhs.z,
}
}
}
impl ops::用于&Point3D的Mul{
类型输出=f32;
fn mul(自身、右侧和点3D)->f32{
self.x*rhs.x+self.y*rhs.y+self.z*rhs.z
}
}
//ops的标量impl::这里的Mul
#[cfg(测试)]
模试验{
使用超级::*;
#[测试]
fn添加点3D(){
设point1=Point3D{
x:1.0,
y:2.0,
z:3.0,
};
设point2=Point3D{
x:4.0,
y:5.0,
z:6.0,
};
让结果=&point1+&point2;
断言(
结果,,
Point3D{
x:5.0,
y:7.0,
z:9.0
},
“使用{:?}和{:?}测试加法”,
第1点,
第2点
);
}
#[测试]
fn减法_点_3D(){
设point1=Point3D{
x:1.0,
y:2.0,
z:3.0,
};
设point2=Point3D{
x:4.0,
y:5.0,
z:6.0,
};
让结果=&point1-&point2;
断言(
结果,,
Point3D{
x:-3.0,
y:-3.0,
z:-3.0
},
“使用{:?}和{:?}测试减法”,
第1点,
第2点
);
}
#[测试]
fn point3D_point3D_乘法(){
设point1=Point3D{
x:1.0,
y:2.0,
z:3.0,
};
设point2=Point3D{
x:4.0,
y:5.0,
z:6.0,
};
让结果=&point1*&point2;
断言(
结果是32.0,
“用{:?}和{:?}测试乘法”,
第1点,第2点
);
}
/*
#[测试]
fn点3D_标量_乘法(){
设point1=Point3D{x:1.0,y:2.0,z:3.0};
设标量=3.5;
让结果=&point1*&scalar;
assert_eq!(结果,点3d{x:3.5,y:7.0,z:10.5},“测试与{:?}和{:?}的乘法”,点1,标量);
}
*/
}

我想在乘法特性中使用泛型,这样,如果我传递另一个
Point3D
类,它将实现点积,但是如果我传递一个基本数字类型(整数,f32,无符号整数,f64),它将
x
y
z
乘以标量值。我该怎么做呢?

你的意思是这样的吗

impl ops::Mul<f32> for &Point3D {
    type Output = Point3D;
    fn mul(self, rhs: f32) -> Point3D {
        Point3D {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs
        }
    }
}

谢谢你的回复。但不是。我不想为每种类型实现乘法运算符,例如(uint、int或f64)。我希望,理想情况下,一个实现可以与我的Point3D结构一起工作,或者我可以使用两个。一种用于Point3D*Point3D的特定情况,另一种用于所有其他数值类型。这就是我的问题。如何以通用方式编写乘法特征。
let point = Point3D { x: 1.0, y: 2.0, z: 3.0};
let result = &point * 4.0;