gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:30:31 24 4
gpt4 key购买 nike

我正在尝试通过实现一些基本数据结构来学习 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com