Answer the question
In order to leave comments, you need to log in
How to fix used of moved value error?
When hovering over row and temp[col] vec[row][col] = temp[col]
it gives out Use of moved value which I can't figure out where it's happening. The same with "r" from vec[r].first()
also the compiler gives index out of bounds: the len is 2 but the index is 2
'E' => { (0..rows).for_each(|row| {
let temp = (0..cols)
.rev()
.map(|col| vec[row][col])
.filter(|&x| x != 0)
.collect();
let mut temp = tilt_sequence(temp, cols);
temp = temp.into_iter().rev().collect();
(0..cols).for_each(|col| vec[row][col] = temp[col]);
});
while (0..rows).map(|r| vec[r].first().unwrap()).all(|&x| x == 0) {
(0..rows).for_each(|r| {vec[r].remove(0);});
};
},
Answer the question
In order to leave comments, you need to log in
temp = temp.into_iter().rev().collect();
(0..cols).for_each(|col| vec[row][col] = temp[col]);
temp
owns some memory (0..cols).for_each
iterates and calls the cols function passed to it once |col| vec[row][col] = temp[col]
, and since temp is captured by the closure by value (in fact, it is part of the closure), at the end of the iteration of the loop inside for_each it is also itself the closure is dropped. index out of bounds: the len is 2 but the index is 2This is not a compilation error, but a runtime panic, which says that you have gone beyond the slice boundary
while (0..rows).map(|r| vec[r].first().unwrap()).all(|&x| x == 0) {
(0..rows).for_each(|r| {vec[r].remove(0);});
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question