0
votes

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.

1

1 Answers

1
votes

Using

*trans_matrix.rows.get_mut(j * i) = self.rows[i * j];

works, but is only a workaround. I don't know how long it will take until IndexMut works as intended, or if it already does, but this was the preferred way some time ago.