6 ms·
The thing is in Rust this is totally ok. Is NOT un-idiomatic to mix imperative + functional paradigms in the same block.
by mamcx 4y ago
The thing is in Rust this is totally ok. Is NOT un-idiomatic to mix imperative + functional paradigms in the same block.
- throwaway894345 4y agoAgreed, 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.
- nomel 4y agoI disagree. If I can avoid an index variable, I’m going to have less bugs since I won’t have to index.
- throwaway894345 4y agoI'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.
- nomel 4y agoAgreed. That's why I enjoy iterators, and use them whenever possible. I think they're important.
- throwaway894345 4y agoBut earlier you claimed the for-loop version required index variables and indexing into iterators/vectors/etc ... ?
- nomel 4y agoI 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"
- MauranKilom 4y agoIt 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").
- throwaway894345 4y agoYes, 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. :)
- lr1970 4y ago> 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.
- deleted 4y ago[deleted]
- mustache_kimono 4y ago> 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.
- throwaway894345 4y agoI 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).
- mustache_kimono 4y agoAgreed. Your example is a pretty pathological case, but yeah.
- efnx 4y agoThe 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.
- lvass 4y agoThe 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?
- steveklabnik 4y agoRust doesn't guarantee TCO and so recursion is a bit dicer than in many functional languages.
- throwaway894345 4y agoIn addition to Steve's sibling comment, I'm responding narrowly to iterators versus for-loops. Recursion is out of scope.