News for hackers. Stuff that matters.
Re:Rust – A hard decision pays off by mamcx on Wednesday August 24, 2022 @12:50PM (#32582183)
The thing is in Rust this is totally ok. Is NOT un-idiomatic to mix imperative + functional paradigms in the same block.
Re:Rust – A hard decision pays off by throwaway894345 on Wednesday August 24, 2022 @01:06PM (#32582448)
Agreed, but 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 problems, and on the more complex end you’re writing imperative stuff anyway. Iterators certainly aren’t a bad thing, but they’re not the game changer that Go-critics made them out to be in all of the Rust-vs-Go debates.
Re:Rust – A hard decision pays off by mustache_kimono on Wednesday August 24, 2022 @03:33PM (#32584614)
> Agreed, but I think people make too big a deal about iterators.

I may just have more time to dick around, but I've been forcing myself to write everything as an iterator in Rust (instead of a for loop) just to learn the paradigm and, honestly, it's been pretty great.

First, there is a performance impact. You may think this would be small, but, in my experience, it can be relatively large. There must be several reasons of which I am ignorant, but they probably include mutable no alias inside closures, bounds check elision, etc.

Second, there is a mutability advantage to just using filter/flatten/map/collect for 90% of your use cases.

Third, the more advanced iterator methods will cover the other 5-10% of your use cases, and will make your code much easier to read, once you understand how to use them (if you're trying to learning something/anything more complex at the same time, save your brain power!). I don't have a single traditional for loop in my personal projects.

My take on iterators is, for iterator/Rust beginners (like me!/perhaps not you) -- the best way to learn is to get it working as imperative code, and when you have the time, go back a rewrite as an iterator. In the beginning, it's super frustrating. But you'll get better and better and, after awhile, you'll much, much prefer writing as an iterator. You won't even think of using a for loop.

Re:Rust – A hard decision pays off by throwaway894345 on Wednesday August 24, 2022 @04:58PM (#32585659)
I mean, I definitely have tried to rewrite stuff as iterators even when it isn't convenient. I only got to that working example by dedicating a few hours with the good people on the Rust Discord trying to make it work (a lot of seasoned Rustaceans also weren't able to get it working, it was a collaborative effort that took a fair amount of time and rhapsodizing). And even then, the result is pretty ugly (I don't think many seasoned Rustaceans would call it "idiomatic" or advise the first version over the second).
Re:Rust – A hard decision pays off by mustache_kimono on Wednesday August 24, 2022 @06:09PM (#32586445)
Agreed. Your example is a pretty pathological case, but yeah.
Re:Rust – A hard decision pays off by efnx on Wednesday August 24, 2022 @04:36PM (#32585392)
The selling point of iterators isn’t that they replace for loops, it’s that you can make new iterators by implementing a trait. That at way downstream users can use your iterators in a for loop. Of course you also can use a more functional style if you like.
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.

Re:Rust – A hard decision pays off by lvass on Wednesday August 24, 2022 @01:43PM (#32582993)
The first version isn't what I'd write in a purely functional language either, a recursive version would be much saner. Isn't recursion idiomatic in rust?
Re:Rust – A hard decision pays off by steveklabnik on Wednesday August 24, 2022 @01:51PM (#32583139)
Rust doesn't guarantee TCO and so recursion is a bit dicer than in many functional languages.
Re:Rust – A hard decision pays off by throwaway894345 on Wednesday August 24, 2022 @03:27PM (#32584522)
In addition to Steve's sibling comment, I'm responding narrowly to iterators versus for-loops. Recursion is out of scope.