You can’t really blame that on rust.
What could go wrong trying to solve AoC in Rust?
Submitted 11 months ago by MaliciousKebab@sh.itjust.works to programmer_humor@programming.dev
Comments
Schmeckinger@lemmy.world 11 months ago
MaliciousKebab@sh.itjust.works 11 months ago
Yeah ngl it’s very ugly. But hey as long as it works it’s not stupid amirite?
xlash123@sh.itjust.works 11 months ago
Rust borrows a lot of it’s design from functional programming languages like Haskell, which has its good and bad. You could also choose to implement this behavior iteratively like typical C programs, but that tends to be ugly in other ways.
Personally, I’ve grown fond of the functional style. You see it in other places too, like the higher order functions in JavaScript. What’s good about them in Rust is you still get amazing performance due to zero-cost abstraction. Trying to implement it yourself would likely be slower, so use them any chance you get.
GeniusIsme@lemmy.world 11 months ago
Show the alternative, I’ll have a good laugh.
janAkali@lemmy.one 10 months ago
It’s Nim, but I really doubt you can’t do this in Rust:
var seeds = lines[0].split(":")[1].splitWhitespace().mapIt(it.parseInt)
Full solution: codeberg.org/Archargelod/…/solution.nim
GeniusIsme@lemmy.world 10 months ago
Ahh, it is the same thing. Rust example surely has some cruft, but mostly for the better. I’m sure not all of it is needed.
chinstrap@lemmy.ml 11 months ago
Java 2. World is full of wonders
crispy_kilt@feddit.de 11 months ago
Java 2 didn’t have streams nor iterator combinatorics, not sure what you mean?
chinstrap@lemmy.ml 11 months ago
i didn’t mean as a version. I meant as overuse of streams
m_f@midwest.social 11 months ago
The
collect
’s in the middle aren’t necessary, neither is splitting by": "
. Here’s a simpler versionIt is simpler to bang out a
[int(num) for num in text.splitlines()[0].split(’ ')[1:]]
in Python, but just shows the happy path with no error handling, and does a bunch of allocations that the Rust version doesn’t. You can also get slightly fancier in the Rust version by collecting into aResult
for better error handling and all that.MaliciousKebab@sh.itjust.works 11 months ago
Yeah I was trying to do something like reading the first line by getting an iterator and just looping through the other lines normally, since first line was kind of a special case but it got messy quick. I realized halfway that my collects were redundant but couldn’t really simplify it. Thanks
sukhmel@programming.dev 10 months ago
Also,
anyhow::Context
provides a convenient way to turnOption<T>
andResult<T, Into<anyhow::Error>>
intoanyhow::Result<T>
Like this: