我试图通过实现一些基本的数据结构来学习Rust。在这种情况下,是一个Matrix。
struct Matrix<T> {
pub nrows: uint,
pub ncols: uint,
pub rows: Vec<T>
}现在,我想使用以下函数转置一个Matrix:
pub fn transpose(&self) -> Matrix<T> {
let mut trans_matrix = Matrix::new(self.ncols, self.nrows);
for i in range(0u, self.nrows - 1u) {
for j in range(0u, self.ncols - 1u) {
trans_matrix.rows[j*i] = self.rows[i*j]; // error
}
}
trans_matrix
}但我在标线上看到了这个错误:
error: cannot move out of dereference (dereference is implicit, due to indexing)
error: cannot assign to immutable dereference (dereference is implicit, due to indexing)因此,我必须做的是使rows of trans_matrix变,并以某种方式修复取消引用错误。我怎么才能解决这个问题?谢谢。
发布于 2014-09-28 13:32:34
使用
*trans_matrix.rows.get_mut(j * i) = self.rows[i * j];有效,但只是一种解决办法。我不知道IndexMut需要多长时间才能按预期工作,或者它是否已经正常工作,但这是不久前首选的方法。
https://stackoverflow.com/questions/26085254
复制相似问题