我正在尝试使用生命周期来实现一些特征,但像往常一样,我正在与借用检查器作斗争。
我的性格是这样的:
pub struct Matrix<T> {
pub cols: usize,
pub rows: usize,
pub data: Vec<T>
}
impl<'a, 'b, T: Copy + Mul<T, Output=T>> Mul<&'b T> for &'a Matrix<T> {
type Output = Matrix<T>;
fn mul(self, f: &T) -> Matrix<T> {
let new_data : Vec<T> = self.data.into_iter().map(|v| v * (*f)).collect();
Matrix {
cols: self.cols,
rows: self.rows,
data: new_data
}
}
}
游乐场链接
这给出了以下错误:
error: cannot move out of borrowed content
我想我明白为什么会发生这种情况:我在借用自我,所以我无法复制数据。我该如何解决这个问题?