Could you please tell me what is the difference here?
Here there is no difference as Stargateur notes. To know what's happening, you simply need to follow the white rabbit trait implementation: Rust's for loop "simply" calls IntoIterator on the "RHS". Now if we go down the list of implementors
It's not documented per-se but looking at the code it just calls self.iter(), so here we do have the confirmation that Stargateur is correct, &Vec and Vec::iter do the exact same thing
The documentation is a bit terse but it links to std::slice::Iter which is "Immutable slice iterator", not necessarily super helpful in and of itself but the trait implementation is pretty clear
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T
}
So Vec<T>::iter -> Iter<T> -> Iterator<Item=&a>, meaning when you .iter() a vector (or you iterate an &Vec) you iterate on immutable references to the items. And since iter takes &self (and &Vec is obviously a reference) it also means that the iteration only borrows the vector, so once you're done iterating the vector is still there unchanged.
&mut Vec and Vec::iter_mut
Though you didn't mention it that's the second iterator, it's similar to the one above except it yields a std::slice::IterMut which
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T
}
so instead of yielding immutable references to items it yields mutable ones, which means you can modify items in-place, e.g. increment them, pretty cool.
So we come to this, and if you expand the definition you see essentially this:
impl<T> IntoIterator for Vec<T> {
type Item = T
type IntoIter = IntoIter<T, A>
pub fn into_iter(self) -> IntoIter<T, A>
Creates a consuming iterator, that is, one that moves each value out of the vector (from start to end). The vector cannot be used after calling this.
Which is pretty self-explanatory: if you iterate on the Vec directly it consumes the vector, meaning you will not be able to use it afterwards.
In return, however, it moves the ownership of the vector's items into the iterator, which provides more flexibility.