I'm confused as to when to use references. I have a vector and I'd like to iterate through the elements inside it.
let v = vec![1, 2, 3, 4, 5];
I can do this using a for
loop in two ways:
Method 1
for element in &v {
println!("{}", element);
}
Method 2
for element in v {
println!("{}", element);
}
In method 1, I use a reference to access the contents of the vector v
. Both examples compile and give the desired output. Which method to use and why?