0

Is there any difference in the following approaches to iterate through a vector? Both methods successfully iterate.

let vo = vec![30, 50, 70, 80];

Method 1

for uu in vo.iter() {
    println!("uu {}", uu);
}
println!("vo 1 {:?}", vo);

Method 2

for uu in &vo {
    println!("{}", uu);
}
println!("vo 2 {:?}", vo);
Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
Hendry Lim
  • 1,709
  • 1
  • 16
  • 34

1 Answers1

1

No difference, no.

The second one is impl<'a, T> IntoIterator for &'a Vec<T>, and it just call the first one (Vec::iter). Since the method is so short, there's roughly 100% chances it's going to get inlined and you'll get the same result (with an intermediate function call if you're compiling without optimisations but that's about it).

Masklinn
  • 23,560
  • 2
  • 21
  • 39