My Advent of Code solutions in Rust
Here are some (quite random) things I have learned about Rust through solving the Advent of Code puzzles.
Feature/function | Day | Description |
---|---|---|
Iterator::filter_map |
3 | Instead of filtering and then mapping, you just return an optional and all None values are automatically filtered out |
Itertools::tuples() |
3 | Using this seemed like magic to me: you can transform an iterator like [s1, s2, ...] into [(s1, s2, s3), (s4, s5, s6), ...] just by adding .tuples() before a .map(|(s1, s2, s3)| ... ) and it will just work! But keep in mind that extra iterator elements that do not fit into this structure are just ignored. |
str::split_once(&self, delimiter) |
4 | This function returns a a tuple, so it was perfect for splitting a string on a delimiter once. |
Vec::windows(&self, usize) |
6 | No, not the OS. This example explains what this function does well: [a,b,c].windows(2) turns into [[a,b], [b,c]] . It creates all possible "windows" of size 2 in from the vector. Very useful for that days task. |
Iterator::inspect(|v| { dbg!(v); }) |
6 | This is how you debug (i.e. print out all values of each element in iterator) iterators. Super useful, I wish I had known of it sooner! |