I'm trying to learn Rust by implementing a few basic data structures. In this case, a Matrix.
struct Matrix<T> {
pub nrows: uint,
pub ncols: uint,
pub rows: Vec<T>
}
Now, I want to transpose a Matrix using this function:
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
}
But I get this error on the marked line:
error: cannot move out of dereference (dereference is implicit, due to indexing)
error: cannot assign to immutable dereference (dereference is implicit, due to indexing)
So what I have to do is make the rows of trans_matrix mutable, and somehow fix the dereference error. How can I solve this? Thanks.