I'm trying the advent of code in Rust this year to learn Rust. I'm not trying to be fast and place on the leaderboards (which also require working at midnight, which I'm mostly not interested in doing), I'm trying to be somewhat elegant and learn new things in Rust. The documentation is live here.
I highly recommend loading this up in a good editor, like Visual Studio Code or VIM with the ALE plugin. It will add type information to all inferred types, autocomplete, show documentation, etc.
Use:
cargo fmt
cargo clippy --all
You should have Rust 1.74+ to use the Cargo.toml
config for clippy. If you want
to auto-fix anything, you can:
cargo clippy --fix --allow-dirty --allow-staged
I also looked for removable features using unused-features, both to speed up compilation and it helped removed a small dependence on unicode in regex.
Use:
cargo test
Useful flags include -- --nocapture
and --bin <NUMBER>
for just one set of tests.
If you have cargo-nextest
(say, from brew install cargo-nextest
), then
cargo nextest run
also works.
Download the input files to input/<number>.txt
. For example, input/01.txt
.
Use:
cargo run -r --bin 01
(-r
for release mode highly recommended for some problems, like 05
!)
You can build with:
cargo docs --no-deps
This is mostly one file per project, with a few shared helpers on a small
number of days. I'm not worrying about visibility or nice error handling since
these are one-shot "scripts". I even played with the script feature in the
nightlies (01
supports it), but I wanted cargo fmt
and cargo clippy
(and
then cargo test
), so I went with the classic project-based approach.
Features used in each vary. For example, 05
has an optional progress bar
(opt-out). Over time, I've been cleaning up the older problems based on what
I've learned in newer problems, so looking at the history for a file might be
instructive. I started using external crates like itertools
& derive_more
around 10-12 or so, but backported a lot of the cleanups later. I added a few
tools for several problems in the aoc2023 crate, but only a small handful use
it.
A few of the crates I'm using or have used:
cached
: Python'sitertools.cache
basicallyderive-new
: Powerfulnew
creation (supports default, unlikederive_more
'sConstructor
).derive_more
: Adds useful derives not part of the stdlib (likeAdd
)grid
: A simple 2D array libraryindexmap
: Ordered mapindicatif
: Progress barsintervalium
/gcollections
:IntervalSet
log
,env_logger
,test-log
: logging facilitiesnum
: Neededlcm
in a problem.pest
/pest_derive
: A PEG parserpetgraph
/rustworkx-core
: Graph tools, similar to networkx for Pythonrayon
(not actively used): Easy multithreadingregex
: Input parsing via regular expressionsstrum
: Powerful enum tools like conversion with strings & iteration over enums
Also see Blessed.rs, a curated list of good Rust libraries.
I added fairly extensive docs to 13
to try cargo doc
. Other days have some intro text,
but little in the way of inline docs.
More links: