我的函数应该返回自我引用还是自我的新副本?

问题描述 投票:0回答:0
use Vec3::Vec3;

struct Matrix3x3
{
    _array: [f64; 9],
}

impl Matrix3x3
{
    pub fn new(m: [f64; 9]) -> Self
    {
        Matrix3x3 { _array: m}
    }

    pub fn from_column_vectors(&self, cx: &Vec3, cy: &Vec3, cz: &Vec3) -> Matrix3x3
    {
        let cx1 = cx.normalized();
        let cy1 = cy.normalized();
        let cz1 = cz.normalized();

        self._array[0] = cx1.x;
        self._array[1] = cx1.y;
        self._array[2] = cx1.z;

        self._array[3] = cy1.x;
        self._array[4] = cy1.y;
        self._array[5] = cy1.z;

        self._array[6] = cz1.x;
        self._array[7] = cz1.y;
        self._array[8] = cz1.z;

        return Rototranslation::new(self._array);
    }

    pub fn from_row_vectors(&self, rx: &Vec3, ry: &Vec3, rz: &Vec3) -> &Matrix3x3
    {
        let cx1 = rx.normalized();
        let cy1 = ry.normalized();
        let cz1 = rz.normalized();

        self._array[0] = cx1.x;
        self._array[1] = cy1.x;
        self._array[2] = cz1.x;

        self._array[3] = cx1.y;
        self._array[4] = cy1.y;
        self._array[5] = cz1.y;

        self._array[6] = cx1.z;
        self._array[7] = cy1.z;
        self._array[8] = cz1.z;

        return self;
    }
}

在上面的源代码中,

from_column_vectors()
将 self 的副本作为新对象返回。另一方面,
from_row_vectors()
返回对自身的引用。

在性能关键应用的情况下,我应该练习哪个选项?

解释为什么。

class object rust clone
© www.soinside.com 2019 - 2024. All rights reserved.