Learning Rust via Advent of Code
forrestthewoods.com
Learning Rust via Advent of Code
1–10 of 85 posts
Re: Learning Rust via Advent of Code
#2Is it limiting?
Should this be used as a complement to a larger toy project?
Re: Learning Rust via Advent of Code
#3> Helper Lambdas
> I like lambdas in C++11. I use them regularly for small helpers that exist solely within a function.
`move` lambdas might work better? The default tries to infer based on usage but often fails. A `move` lambda lets you do something similar to C++'s capture lists (though more verbose / cumbersome).
> BinaryHeap
> The standard library provides a max-heap. I regularly needed a min-heap. I got one by making a custom struct with custom compare function.
std::cmp::Reverse
> I wonder if there is a nice macro crate to help with this? I'd love to write: compare!(a, b, year, month, day, hour, minute);
sort_by_key?
> I wish there was an f16 half-precision float. There's a good internals thread on minifloats that makes me think f16 will happen eventually.
A problem's there is there are at least two "standard" f16 (IEEE and ML)
> There is NonZeroU32. It's similar-ish, but only works for zero. There isn't a Non255U8 or, thankfully, Non4294967296U32.
They're built from the unstable `NonZero` (and the unstable and unsafe ZeroAble).
You could build a struct on top of NonZero which swaps zero and your sentinel value.
Re: Learning Rust via Advent of Code
#4It seems like we ran into a lot of the same pain points like parsing, lack of a min-heap, etc.
Regarding HashMap initialization the best way I'm aware of to do what you are looking for is with lazy_static[0]. Maybe not very clean or idiomatic though.
For sorting I found the best solution to that problem was using sort_by_key with a tuple like (a.year, a.month, a.day, a.hour, a.minute).
[0]: https://github.com/rust-lang-nursery/lazy-static.rs#example
Re: Learning Rust via Advent of Code
#5Re: Learning Rust via Advent of Code
#6What is HN-users view of learning a programming language through this kind of problem-solving? Is it limiting? Should this be used as a complement to a larger toy project?
Re: Learning Rust via Advent of Code
#7What is HN-users view of learning a programming language through this kind of problem-solving? Is it limiting? Should this be used as a complement to a larger toy project?
These types of problems will give you a good intro to a language, but won't introduce you to the more advanced libraries which you'll likely need in a professional context (e.g. threading/mutexs, tcp/udp, etc).
Re: Learning Rust via Advent of Code
#8Regarding parsing, a lot of times the input format is unnecessarily complex. In "real-life", wouldn't you start by sanitizing your input? For me, search/replace and column-edits have been enough to avoid regexps entirely.
Re: Learning Rust via Advent of Code
#9I'm trying to do the exact same thing (learning Rust through solving the AoC) but I'm not done yet. Regarding parsing, a lot of times the input format is unnecessarily complex. In "real-life", wouldn't you start by sanitizing your input? For me, search/replace and column-edits have been enough to avoid regexps entirely.
input
.lines()
.map(|s| s.trim())
.filter(|l| l.len() > 0)
.map(CustomType::from)
.collect::>();
or for grid-like puzzels input
.lines()
.map(|s| s.trim())
.filter(|l| l.len() > 0)
.map(|l| l
.chars()
.map(CustomType::from)
.collect::>()
)
.collect::>();
0: https://github.com/k0nserv/advent-of-rust-2018Re: Learning Rust via Advent of Code
#10What is HN-users view of learning a programming language through this kind of problem-solving? Is it limiting? Should this be used as a complement to a larger toy project?
On the other hand, the author of that post seems to have used a lot of more complex language features, did profiling, debugging, made use of many libraries etc - a lot of extra stuff that wasn't necessary to give correct answers but is crucial in 'real world' programming.