News for hackers. Stuff that matters.
Re:Rust – A hard decision pays off by nomel on Wednesday August 24, 2022 @01:34PM (#32582858)
I disagree. If I can avoid an index variable, I’m going to have less bugs since I won’t have to index.
Re:Rust – A hard decision pays off by throwaway894345 on Wednesday August 24, 2022 @03:25PM (#32584502)
I'm not sure what you mean. You don't have to deal with index variables in a for loop:

    for item in iterator {
        vector.push_back(foo(item));
    }
You're not explicitly indexing the source iterator or the destination vector at any point.
Re:Rust – A hard decision pays off by nomel on Wednesday August 24, 2022 @03:41PM (#32584713)
Agreed. That's why I enjoy iterators, and use them whenever possible. I think they're important.
Re:Rust – A hard decision pays off by throwaway894345 on Wednesday August 24, 2022 @04:11PM (#32585114)
But earlier you claimed the for-loop version required index variables and indexing into iterators/vectors/etc ... ?
Re:Rust – A hard decision pays off by nomel on Wednesday August 24, 2022 @07:33PM (#32587308)
I clearly said the opposite, in response to, what appeared to be, your suggestion that indexing isn't so bad:

"I think people make too big a deal about iterators. The simple for loops that they’re fit to replace aren’t that hard to read or write, so they aren’t causing real problem"

Re:Rust – A hard decision pays off by MauranKilom on Wednesday August 24, 2022 @08:13PM (#32587639)
It seems like you imply that iterators = range-for and "simple loops" = indexing, which is clearly not what the previous poster has in mind (which was "range-for = imperative loops, iterators = functional style, simple loops = trivial loop body").
Re:Rust – A hard decision pays off by throwaway894345 on Wednesday August 24, 2022 @08:37PM (#32587813)
Yes, I was responding to:

> I disagree. If I can avoid an index variable, I’m going to have less bugs since I won’t have to index

Which (given thread context) sounds pretty clearly like he's advocating against `for item in iterator {...}` and in favor of iterator chains. If there's another way of reading that comment, I'm not seeing it. :)

Re:Rust – A hard decision pays off by lr1970 on Wednesday August 24, 2022 @05:21PM (#32585942)
> If I can avoid an index variable, I’m going to have less bugs since I won’t have to index.

Not only that. In safe Rust vector indexes are always bound-checked that slows execution a little bit. Iterators over a vector, on the other hand, do not suffer from bound-check overhead.