Reader lines iterator call a second time on conditions


#1

Hello,

I met a borrow issue when I do something like :

//  reader is a BufRead struct
let lines = reader.lines ();
for line in lines {
  ///....
  if lines.len () == 0 {
    lines.next ();
  }
  //...
}

Please advices.

Regards

Corentin


#2

By calling lines.next() you’re mutating the iterator while iterating. It’s not clear what you’re really trying to do, but simply replacing that by continue might be what you want. You should look into filter though, which is a more elegant and effective way to achieve that.


#3

In Rust only while loop gives flexibility. The for loop intentionally takes ownership of the iterator and does not give any control.

while let Some(item) = iter.next() {
}