Live data from Hacker News

Learning Rust via Advent of Code

forrestthewoods.com

1–10 of 85 posts

Re: Learning Rust via Advent of Code

#3
Some comments having flown through the article

> 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

#4
As someone who also completed Advent of Code in Rust this year this was very interesting. I agree it is a great way to learn more about a new language. Thanks for sharing!

It 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

#6
post #2

What 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?

I've been starting to learn Rust and found the post quite useful. I think the inherit constraints with these sorts of challenges allow for time to think of creative ways to use a language's fundamentals, rather than focusing too much on architecture. It forces learning through immersion, akin to being dropped in a foreign land without knowing the language.

Re: Learning Rust via Advent of Code

#7
post #2

What 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?

I would say it depends on what you are trying to learn.

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

#8
I'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.

Re: Learning Rust via Advent of Code

#9

I'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.

Agreed. Most of my solutions[0] use something simple like

    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-2018

Re: Learning Rust via Advent of Code

#10
post #2

What 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?

It depends on the things that you want to learn and amount of extra work you want to put into it. For instance, I used Go for the most of AoC 2018 problems and I had always been using C/C++ in algorithmic contests when execution time mattered. And yet, I wouldn't say I know any of these languages good enough to write a production-grade system because I only learned usage patterns common for small, write - run once - throw away applications.

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.

Post reply on HN