Hey folks, I'm learning about the Rust language. One simple thing I'm trying to do is iterate over a Vec, in a for loop, and then access the Vecafter the for loop.
let x = vec![5,6,17,2];
for val in x {
println!("{}", val);
}
println!("{}", x[0]);
The above code yields the following error:
borrow of moved value: x value borrowed here after move
I figured by cloning the Vec with x.clone(), I could work around this.
let x = vec![5,6,17,2];
for val in x.clone() { // edited this line
println!("{}", val);
}
println!("{}", x[0]);
This does work, but I'm confused at why the initial example fails. Why does x get borrowed by the for loop, but never given back to the parent scope?