Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by tested23 on Saturday December 31, 2022 @01:53PM
(#34199464)
Ugh, you are right but then someone comes and uses this to rationalize not including things like map, filter and reduce in a language because they are supposedly too complicated and you can just do it with a for loop
[ Parent
| on HN ]
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by baby on Saturday December 31, 2022 @01:58PM
(#34199521)
I work in a Rust codebase that uses a lot of functional functions, and I’ll say this: on average the imperative style takes less lines of code and less indentation. I also find it more readable personally, and idiomatic.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by nicoburns on Saturday December 31, 2022 @02:26PM
(#34199776)
Functional iteration is good for the same reason we use for loops over while loops, and while loops over goto: they are more constrained, more clearly communicate intent, and are therefore easier to reason about.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by josephg on Saturday December 31, 2022 @03:22PM
(#34200338)
Sure but it’s easy to go overboard with this stuff. Reduce (fold) especially can be pretty hard to read in hairy situations.
My general rule is that if you need fewer lines of code to implement your logic with a simple for loop, you probably should.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by nicoburns on Sunday January 01, 2023 @10:16AM
(#34206911)
Yeah, I agree with that. Especially reduce/fold, which I find is almost always better written as loop. Filter would be a good example of the opposite for me: almost always much clearer written functionally.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by jamincan on Sunday January 01, 2023 @04:29PM
(#34211153)
I find I frequently use a combination - use map/filter to setup an iterator, and then reduce it in a loop.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by josephg on Saturday December 31, 2022 @03:17PM
(#34200308)
Just because we’re on the topic of performance: the rust optimizer can sometimes generate better code if you use map / filter / etc. The slice iterator in any context is a huge win over manual array iteration because it only needs to do bounds checking once.
Javascript (v8, last I checked) is the opposite. Simple for loops almost always outperform anything else.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by duckerude on Saturday December 31, 2022 @05:33PM
(#34201437)
I've seen cases where an iterator was better, but I've also seen gains from using an imperative loop with manual indexing. Loop conditions and the occasional assertion can be enough to elide bounds checks. (Though sometimes the compiler gets too paranoid about integer overflow.)
Most of the time you should just write whatever's clear/convenient but sometimes it's worth trying both and scrutinizing godbolt.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by baby on Sunday January 01, 2023 @03:50AM
(#34204766)
This is premature optimization imo. Unless you’re using par_iter
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by josephg on Sunday January 01, 2023 @04:41AM
(#34205007)
Without knowing the domain, you have no way of knowing that. Eg in the rendering pipeline in the React codebase, I’d be extremely leery of using map/filter/reduce because of how performance sensitive that code is.
Re:Should Small Rust Structs be Passed by-copy or by-borrow?
by int_19h on Saturday December 31, 2022 @09:55PM
(#34203121)
I suspect this has more to do with the lack of first-class sequence comprehensions in the syntax. If you had to write imperative style, but all loops were HOFs, it would hardly be ergonomic, either. OTOH a good query language is much more readable.