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
H2CO3
2
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?
alice
5
There is a slight preference for using
for i in &t {
println!("{}", i);
}
3 Likes
system
Closed
6
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.