【问题标题】:How to implement ops::Mul on a struct so it works with numerical types as well as another struct?如何在结构上实现 ops::Mul 使其适用于数字类型以及另一个结构?
【发布时间】:2020-11-25 00:49:47
【问题描述】:

我已经实现了一个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);
    }
    */
}

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

【问题讨论】:

    标签: rust traits


    【解决方案1】:

    你的意思是这样的吗?

    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
            }
        }
    }
    

    这将允许您执行以下操作:

    let point = Point3D { x: 1.0, y: 2.0, z: 3.0};
    let result = &point * 4.0;
    

    【讨论】:

    • 感谢您的回复。但不是。我不想为每种类型实现乘法运算符,例如(uints、ints 或 f64)。我希望,理想情况下,一个实现可以与我的 Point3D 结构一起使用,或者我可以使用两个。一种用于 Point3D * Point3D 的特定情况,然后是所有其他数字类型的第二种情况。这就是我的问题所在。如何以通用方式编写乘法特征。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2020-12-30
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多