Error

"cannot move out of index of Vec" — How to Fix

Fix the 'cannot move out of index of Vec' error by borrowing the element with & or removing it with .remove() instead of indexing directly.

The error occurs because indexing a Vec returns a value by move, not a reference, so you cannot use that value again in the same scope. Change your code to borrow the element using & or use .get() to access it safely without moving ownership.

let vec = vec![1, 2, 3];
// Instead of: let x = vec[0]; // moves the value
let x = &vec[0]; // borrows the value
println!("Value: {}", x);

If you need to remove the element, use .remove() which returns ownership explicitly:

let mut vec = vec![1, 2, 3];
let x = vec.remove(0); // takes ownership
println!("Removed: {}", x);