this post was submitted on 06 Dec 2023
23 points (96.0% liked)
Advent Of Code
770 readers
1 users here now
An unofficial home for the advent of code community on programming.dev!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
AoC 2023
Solution Threads
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 |
Rules/Guidelines
- Follow the programming.dev instance rules
- Keep all content related to advent of code in some way
- If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2023 Day 10])
- When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts
Relevant Communities
Relevant Links
Credits
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
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
Rust
Feedback welcome! Feel like I'm getting the hand of Rust more and more.
I'm no rust expert, but:
you can use
into_iter()
instead ofiter()
to get owned data (if you're not going to use the original container again). Withinto_iter()
you dont have to deref the values every time which is nice.Also it's small potatoes, but calling
input.lines().collect()
allocates a vector (that isnt ever used again) whenlines()
returns an iterator that you can use directly. You can instead passlines.next().unwrap()
into your functions directly.Strings have a method called
split_whitespace()
(also asplit_ascii_whitespace()
) that returns an iterator over tokens separated by any amount of whitespace. You can then call.collect()
with a String turbofish (i'd type it out but lemmy's markdown is killing me) on that iterator. Iirc that ends up being faster because replacing characters with an empty character requires you to shift all the following characters backward each time.Overall really clean code though. One of my favorite parts of using rust (and pain points of going back to other languages) is the crazy amount of helper functions for common operations on basic types.
Edit: oh yeah, also strings have a
.parse()
method to converts it to a number e.g.data.parse()
where the parse takes a turbo fish of the numeric type. As always, turbofishes arent required if rust already knows the type of the variable it's being assigned to.Thanks for making some time to check my code, really appreciated! the split_whitespace is super useful, for some reason I expected it to just split on single spaces, so I was messing with trim() stuff the whole time :D. Could immediately apply this feedback to the Challenge of today! I've now created a general function I can use for these situations every time:
Thanks again!