rust - 矢量索引 : cannot move out of dereference

标签 rust

我正在尝试通过实现一些基本数据结构来学习 Rust。在本例中,是一个 Matrix

struct Matrix<T> {
  pub nrows: uint,
  pub ncols: uint,
  pub rows: Vec<T>
}

现在,我想使用这个函数转置一个矩阵:

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)

所以我要做的是使 trans_matrixrows 可变,并以某种方式修复取消引用错误。我该如何解决这个问题?谢谢。

最佳答案

使用

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

有效,但这只是一种解决方法。我不知道 IndexMut 按预期工作需要多长时间,或者如果它已经按预期工作,但这是前段时间的首选方式

关于rust - 矢量索引 : cannot move out of dereference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26085254/

相关文章:

performance - 我可以在每次除法时禁用检查零除法吗?

arrays - 如何重置所有数组元素?

rust - 是否可以在不解析命令行参数的情况下构造一个 StructOpt Args 对象进行测试?

callback - 如何将方法作为回调传递

rust - 使用1个或多个元素消耗和替换向量中的元素

image - 如何将 DynamicImage 转换为 ImageBuffer?

rust - Rust 中的动态类型实体组件系统

rust - 有没有办法在 Rust 中定义多参数特征?

macos - OS X 上的 Rust 和加载程序路径(@rpath、@loader_path)

rust - 是否可以在 Rust 的函数中定义可选参数?