私は錆の寿命に問題があります。私は以下にたくさんの調整を試みましたが、私は新しいエラーを導入し続けます。私はインデックスをVectorオブジェクトを返すようにします。錆の寿命 - 変数が十分に長く生きていないエラー
私が持っている:
struct Matrix<T> {
num_rows: i32,
num_cols: i32,
data: Vec<T>
}
struct Vector<T> {
data: Vec<T>
}
そして、私は
impl<T: Clone> Index<usize> for Matrix<T> {
type Output = Vector<T>;
fn index(&self, i: usize) -> &Vector<T> {
let index = i as i32;
let start = (index * &self.num_cols) as usize;
let end = (((index + 1) * &self.num_cols) - 1) as usize;
let data_slice = &self.data[start..end];
let data = data_slice.to_vec();
let vector_temp = Vector::<T>::new(data);
return &vector_temp;
}
}
をやろうとしている。しかし、私はまだ完全には錆で寿命をgrokkedいない
error: `vector_temp` does not live long enough
--> src\main.rs:45:17
|
45 | return &vector_temp;
| ^^^^^^^^^^^ does not live long enough
46 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the block at 38:44...
--> src\main.rs:38:45
|
38 | fn index(&self, i: usize) -> &Vector<T> {
| ^
error: aborting due to previous error
error: Could not compile `hello_world`.
を取得しています誰かが私を助けてくれることを望んでいた。ありがとう!