I am going through the "Rust Book" website in order to learn the language for an upcoming job interview. In the chapter on vectors, there are two code samples:
fn main() {
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
}
}
and:
fn main() {
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
}
Now I am wondering, why is it that for the first sample, when we pass the reference to the vector element i into:
println!("{}", i);
but in the sample where we add 50 to each element of the vector, we have to dereference the element with * before we add to the 50?
Why don't/can't we do the following:
fn main() {
let v = vec![100, 32, 57];
for i in &v {
println!("{}", *i); // why don't we have to dereference before we pass to println!?
}
}
or:
fn main() {
let mut v = vec![100, 32, 57];
for i in &mut v {
i += 50; // why can't we just add directly to the reference like this?
}
}
I must have misunderstood what I read, but I thought Rust was able to discern when you need to dereference or not automatically. I guess I don't understand why we need to dereference (or not dereference) in the two samples. The two examples I provided are commented with the specific bits of code I am wondering about.