首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向量索引:不能脱离引用

向量索引:不能脱离引用
EN

Stack Overflow用户
提问于 2014-09-28 13:19:52
回答 1查看 566关注 0票数 0

我试图通过实现一些基本的数据结构来学习Rust。在这种情况下,是一个Matrix

代码语言:javascript
复制
struct Matrix<T> {
  pub nrows: uint,
  pub ncols: uint,
  pub rows: Vec<T>
}

现在,我想使用以下函数转置一个Matrix

代码语言:javascript
复制
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
}

但我在标线上看到了这个错误:

代码语言:javascript
复制
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变,并以某种方式修复取消引用错误。我怎么才能解决这个问题?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-28 13:32:34

使用

代码语言:javascript
复制
*trans_matrix.rows.get_mut(j * i) = self.rows[i * j];

有效,但只是一种解决办法。我不知道IndexMut需要多长时间才能按预期工作,或者它是否已经正常工作,但这是不久前首选的方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26085254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档