Difference between loop over ref of array and iterating over array

Hi,
I have a simple question: what is the difference between looping over a ref of an array and an iterator, especially behind the scenes?

fn main() {
    let t = [0, 1, 2];
    for i in &t {
        println!("{}", i);
    }
    for j in t.iter(){
        println!("{}", j);
    }   
}

Regards
Markus

Nothing. The IntoIterator impl for a reference-to-collection, by convention, yields the exact same iterator as an explicit .iter() call would return.

2 Likes

Here's the behind the scenes, where the IntoIterator implementation for &[T; N] calls .iter().

1 Like

Ok, thanks. Makes sense.

But is there a preferred ideomatic answer or is it just personal preference what you use?

There is a slight preference for using

for i in &t {
    println!("{}", i);
}
3 Likes