this post was submitted on 01 Feb 2024
65 points (91.1% liked)
Programmer Humor
19623 readers
11 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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 that 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 more succinct error handling if you'd like.EDIT: Here's also a version using
anyhow
for error handling, and the aforementionedResult
collecting: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
Also,
anyhow::Context
provides a convenient way to turnOption
andResult>
intoanyhow::Result
Like this:
Edit: line breaks