Difference between `for &item in a.iter()` and `for item in a.iter()`

let a = [10, 20, 30, 40, 50];

for &i in a.iter() {
    // i  i32
    println!("{}", i);
}

for i in a.iter() {
    // i   &i32 
    println!("{}", i);
}

What role does & play here?
Where can I find docmentation about it?

this is a reference pattern

3 Likes

It will be better to understand it with iterator-loops.

IteratorLoopExpression doesn't seem to have any mention of reference patterns.

reference patterns are mostly useful for Copy types (with other types you will get "unable to move out of shared reference" a lot), they let you (for example) pass small integers as arguments to functions like saturating_add without having to manually dereference them with each time.

Well it does say that the induction variable is a PATTERN, so it can be any pattern. There's no need to state that "a PATTERN, including reference patterns". That's obvious.

I don't get what you are asking for here.

yes, but i'm confused as to how linking IteratorLoopExpression is somehow more relevant than just linking to reference patterns directly.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.