5 ms·
Rust iterators are beautiful. If you've ever implemented C++ iterators, you'll be pleasantly surprised at how elegant it is in Rust by comparison (actually you
by eloff 6y ago
Rust iterators are beautiful. If you've ever implemented C++ iterators, you'll be pleasantly surprised at how elegant it is in Rust by comparison (actually you could replace iterators with about any language feature and the above holds true.)
I really like Rust as a language, it's like a lightsaber, an elegant weapon from a more civilized era. Go feels a bit more like a blaster (sorry May 4th was yesterday.)
That said, when it comes to getting things done, the blaster seems more effective. I would chalk that up to garbage collection, goroutines vs async+await, and simplicity of the language in general. That said I use both languages regularly because they have different strengths and weaknesses and one is usually a clearly better option depending on the problem and requirements.
- throwaway894345 6y agoI really admire Rust iterators, especially compared to C++ iterators, but I lament that there is no easy way (that I know of, anyway) to build an iterator from a closure which yields `Option<T>`s. I don’t like that I have to define a type and implement a Trait (in general working with closures is cumbersome in Rust). That said, this isn’t a major problem either and on balance I’m very happy with Rust iterators.
- eloff 6y agoIt's probably possible to create a wrapper to do that. But if you need a closure, you can also just use a struct implementing the trait to manage the state. It's not perfect, but it's so much nicer than in C++.
- creata 6y agoMight you be looking for https://doc.rust-lang.org/std/iter/fn.from_fn.html https://doc.rust-lang.org/std/iter/fn.from_fn.html ?
- throwaway894345 6y agoOh wow, yeah, I think that’s it! Thanks very much!
- forrestthewoods 6y agoWhoa I did not know about this. This would have saved me so much time during Advent of Code…
- eloff 6y agoThanks for sharing! I wish I'd known about this earlier.
- masklinn 6y ago> I lament that there is no easy way (that I know of, anyway) to build an iterator from a closure which yields `Option<T>`s. Could you expand / explain what you actually mean here? Because I can think of multiple interpretations of this prompt which are straight up part of the stdlib, but that I can interpret this description in multiple ways shows the issue.
- throwaway894345 6y agoLet’s say I have a closure `fn() -> Option<T>`—how do I turn that into an Iterator? Maybe it’s possible with the stdlib and I’m just not familiar? EDIT: I think this is it: https://news.ycombinator.com/item?id=27050861 https://news.ycombinator.com/item?id=27050861
- kzrdude 6y agoLet's say std::iter::from_fn didn't exist. Then you'd need to make something that ends the iterator when the function returns none, you could do it like this: std::iter::repeat_with(f).take_while(|elt| elt.is_some()).filter_map(|elt| elt) Where `.filter_map()` removes the Option layer and `repeat_with` creates an initially infinite iterator from your function `f`. Long time ago, `repeat_with` might not have existed. Maybe you'd do something like this instead for that in Rust 1.0.0: `std::iter::repeat(()).map(|_| f())...` You prefer to use std::iter::from_fn now that it exists, of course! To be honest, making a simple iterator adaptor is quite easy too, so you could have a knack at making one you are missing - if you can't find it in std or in the crate itertools already.
- biztos 6y ago> the blaster seems more effective And you have to, er, attack the rebels with an army of highly variable talent and training, which contains maybe one or two actual Jedi Ninjas, so maybe it's better if everyone just uses blasters and nobody cuts off their own arm. I'd love to work in some super elegant language for my own intellectual betterment, but I really hope my next real-world gig uses something like Golang if there are more than a half-dozen devs... ever.
- throwaway894345 6y agoAgreed. I love Rust for my personal projects, but I wouldn’t trust colleagues with it at any place I’ve worked. And it’s not that my colleagues aren’t intelligent or capable, but that Rust’s expressiveness invites bineshedding on the right amount of abstraction or which crate to use or the right way to do errors or how to configure rustfmt or when to just give up a fight with the borrow checker and use clone() or etc. Rust is a really, really cool language—perhaps my favorite language, but I’ve worked in C++ shops and Python shops and I fully expect the same amount of bike shedding at a Rust shop.
- mrweasel 6y agoApparently I’m pretty much alone in thinking that Rust is a clumbsy, messy language. The syntax is all over the place. You get the feeling that there’s no design strategy and than anyone with half an idea is allowed to bolt on features.
- jolux 6y agoRust’s syntax is definitely complicated and that’s one of my least favorite things about it but it’s extremely consistent. What gives you the “all over the place” feeling?
- mrweasel 6y agoI don’t really agree that it consistent, but maybe you’re right that it just complicated. There are so many different syntaxes for all sorts of features that you can’t really hold it all in your head. It’s been a year since I last gave Rust a try, in the mean time I’ve been able to pick Go without much effort. Rust just seem like PHP, it’s fast, featurefull but not thought through.
- nindalf 6y agoYou're assuming that it was difficult to learn because the features weren't thought through. Have you considered that maybe it was because there were genuinely new concepts not present in other languages?
- stouset 6y agoYeah, if anything, Rust feels to me like it's incredibly well thought through. All the pieces fit together well precisely as if they were all thought of as a whole. It's "hard" because it forces you to consider an entirely new constraint of your programs upfront. But (almost) every piece composes nicely with every other piece in a way that makes Rust development extremely pleasant. Golang feels like the opposite, in that none of the parts are actually built to work with one another. Tuple returns exist but there are no language features that allow you to actually work with them. There's a strong type system, but you have to break out of it with interface{} and type punning to solve entire classes of common problems. Type punning exists, but go can't prove exhaustiveness of switch statements so doing this opens further gaping holes. Errors are handled explicitly everywhere, but again there are no tools to handle the repetitive lifting so every program becomes an endless series of error handling occasionally interrupted by bits of program logic. And let's not forget how badly interface pointers interact with nils: comparing a nil interface pointer against nil returns false, so a basic nil-check that works everywhere else fails to actually perform correctly when dealing with interfaces.
- alexhutcheson 6y ago> If you've ever implemented C++ iterators, you'll be pleasantly surprised at how elegant it is in Rust by comparison You probably already know this, but if you're willing to take a dependency on Boost.Iterator, then boost::iterator_facade[1] makes it much easier and less error-prone to implement a standards-conforming iterator, and the pre-built iterator adapters[2] cover lots of common cases, so you don't need to directly implement an iterator at all. Not arguing the relative merits of C++ vs. Rust, just leaving a pointer here for anyone who didn't know about it. [1] https://www.boost.org/doc/libs/1_66_0/libs/iterator/doc/iterator_facade.html#tutorial-example https://www.boost.org/doc/libs/1_66_0/libs/iterator/doc/iter... [2] https://www.boost.org/doc/libs/1_66_0/libs/iterator/doc/index.html#specialized-adaptors https://www.boost.org/doc/libs/1_66_0/libs/iterator/doc/inde...
- obviouslynotme 6y agoRust is a beautiful language. Its only issue is that programming in it is painful for anyone who hasn't followed Modern C++ for a decade and then read everything on Rust, which is just Modern C++ with some niceness from ML thrown on top. I couldn't even begin to imagine onboarding a Jr Engineer to a complex Rust project. I don't even know if it is possible.
- cratermoon 5y agoWhat constitutes "Modern C++"?