Finally learning some Rust - hello photo-backlog-exporter!

Posted on March 9, 2024 with tags , . See the previous or next posts.

Wow that's a long title šŸ˜›

After 4? 5? or so years of wanting to learn Rust, over the past 4 or so months I finally bit the bullet and found the motivation to write some Rust. And the subject.

And I was, and still am, thoroughly surprised. Itā€™s like someone took Haskell, simplified it to some extents, and wrote a systems language out of it. Writing Rust after Haskell seems easy, and pleasant, and you:

  • donā€™t have to care about unintended laziness which causes memory ā€œleaksā€ (stuck memory, more like).
  • donā€™t have to care about GC eating too much of your multi-threaded RTS.
  • can be happy that thereā€™s lots of activity and buzz around the language.
  • can be happy for generating very small, efficient binaries that feel right at home on Raspberry Pi, especially not the 5.
  • are very happy that error handling is done right (Option and Result, not like Goā€¦)

On the other hand:

  • there are no actual monads; the ? operator kind-of-looks-like being in do blocks, but only and only for Option and Result, sadly.
  • thereā€™s no Stackage, itā€™s like having only Hackage available, and you can hope all packages work together well.
  • most packaging is designed to work only against upstream/online crates.io, so offline packaging is doable but not ā€œnativeā€ (from what Iā€™ve seen).

However, overall, one can clearly see thereā€™s more movement in Rust, and the quality of some parts of the toolchain is better (looking at you, rust-analyzer, compared to HLS).

So, with that, Iā€™ve just tagged photo-backlog-exporter v0.1.0. Itā€™s a port of a Python script that was run as a textfile collector, which meant updates every ~15 minutes, since it was a bit slow to start, which I then rewrote in Go (but I donā€™t like Go the language, plus the GC - if I have to deal with a GC, Iā€™d rather write Haskell), then finally rewrote in Rust.

What does this do? It exports metrics for Prometheus based on the count, age and distribution of files in a directory. These files being, for me, the pictures I still have to sort, cull and process, because I never have enough free time to clear out the backlog. The script is kind of designed to work together with Corydalis, but since it doesnā€™t care about file content, it can also double (easily) as simple ā€œfile count/age exporterā€.

And to my surprise, writing in Rust is soo pleasant, that the feature list is greater than the original Python script, and - compared to that untested script - Iā€™ve rather easily achieved a very high coverage ratio. Rust has multiple types of tests, and the combination allows getting pretty down to details on testing:

  • region coverage: >80%
  • function coverage: >89% (so close here!)
  • line coverage: >95%

I had to combine a (large) number of testing crates to get it expressive enough, but it was worth the effort. The last find from yesterday, assert_cmd, is excellent to describe testing/assertion in Rust itself, rather than via a separate, new DSL, like I was using shelltest for, in Haskell.

To some extent, I feel like I found the missing arrow in the quiver. Haskell is good, quite very good for some type of workloads, but of course not all, and Rust complements that very nicely, with lots of overlap (as expected). Python can fill in any quick-and-dirty scripting needed. And I just need to learn more frontend, specifically Typescript (the language, not referring to any specific libraries/frameworks), and Iā€™ll be ready for AI to take over coding šŸ˜…ā€¦

So, for now, Iā€™ll need to split my free time coding between all of the above, and keep exercising my skills. But so glad to have found a good new language!