Advent of Code – An excuse to learn Rust
I had no plans to try Advent of Code this year, or to learn Rust, but I saw a thread on Hacker News and then I started AoC, and then I saw another comment a couple of days in:
After that there was no turning back, I was committed. I was going to do it all in Rust.
- First things first, I found out how to install Rust.
- Set up my text editor
- Began going through their wonderful book (which I'm now about half way through).
- Struggled through the first two problems (that I'd already solved in Python).
- Fought the borrow checker, and began to learn the standard library and type system (efforts ongoing).
- Started building out to modules.
- Used their integrated testing tooling (which is great!).
- Learned to output graphics onto the screen – to solve some of the challenges.
It's been almost two crazy weeks of late nights of panic!("{:?}", reason)
and struggle, but considering the power of the language I've really grown to enjoy it.
There is excellent documentation, which comes alive as soon as you understand the type system well enough to read it. The compiler errors are great, and in particular things like match
statements giving warnings if you don't cover all possibilities – really helpful. Using paradigms like Some(x)
None
rather than allowing null values, also all fantastic.
I think the only thing that takes a significant effort at first is to understand Lifetimes – how they handle the cleanup of memory without manual memory management (like C++) or garbage collection (like Python/Golang/JS). The payoff is huge in terms of fast execution, and memory safety but there are things that I just could not grok in a day. To really get the most out of the language you have to own it (or maybe borrow it... hmmm).
The Advent of code itself has been so cool. I have so much love for the creativity, puzzle creation and interesting references like Quines – and for problems that force me to relive my geometry days from school suddenly breaking out the atan2
function to convert vectors to degrees so I can destroy asteroids! There are plenty of hilarious video links and references mixed into the story they weave with each puzzle too.
If you are thinking about using AoC to learn a language – you should! I cannot pretend that it's been easy for me, but it's been great fun. I would certainly consider doing it again and I hope to use a lot more Rust in 2020!
p.s. You should ask me how proud I am of my little IntCode VM. Everyone should have one.