Mini-project: mdanchored
For a few years now, I’ve been kind of telling myself, on and off, “you should really learn Rust you know, it looks like it has legs and could be rather useful”. In that same way that you tell yourself, as you get older, in an absent-minded kind of way, “You should really settle down now” or “Really! By your age you should have acquired a taste for dark chocolate”. Basically: “do something serious and grown up”. And, while I did settle down, and get to like dark chocolate, I didn’t get around to writing any Rust.
But sometimes you have to make your own opportunities, and I had an idea for a tiny application, and instead of reaching for Go or Python, I reached for Rust.
It was fun, and I wish I’d done it sooner.
(Is Rust “grown up”? I’m not sure, but over time I’ve learned that I like learning the lower-level details of the systems I work with, and Rust seems like a good next step in that process.)
Learning a sprinkling of Rust
Armed only with the Rust website and a five year old first edition of Programming Rust, I set about writing my 250 lines of Rust. I tried to avoid StackOverflow, to avoid copy-pasting code I didn’t understand. Of course, in the end, I copy-pasted code I didn’t understand from the documentation instead. But only once, and I didn’t completely not understand it.
So what did I write? A tiny app to make my markdown ever-so-slightly neater by
moving link references (ie, [my link]: https://foo.com
) to the bottom of their
“section”. It moves them to above the next header or to the bottom of the
markdown file, whichever comes first. Optionally, you can then pass the text
through deno fmt
. (Deno formats markdown quickly and well. Recommended.)
In the end, this turned out to be a nice little project. I learned:
- A bit about borrowing and lifetimes.
- How to pipe data from
stdin
through my own function and then throughdeno fmt
without buffering it all in memory. - How to execute processes with Rust.
- How to write simple tests.
- That rust has multiline strings by default 🌟
- Programming Rust is a really well-written book. Highly recommended.
Overall, I enjoyed writing Rust. I found that, yes, the compiler was difficult
to please. But also that, when I’d finally pleased the compiler, I was pretty
sure the program wouldn’t crash in unexpected locations. Instead, I could see
where the program might crash, because it was in places where I’d chosen to
ignore a piece of safety Rust offered — for example, explicitly using a call
that assumes a value isn’t null
, and panics if it is.
I wonder whether this time spent making the compiler happy might also help avoid other bugs. At the same time as following trails of memory to please the compiler, I also ended up following lines of logic. So I like to think that the compiler’s demand that I think more deeply about memory semantics also encouraged me to ponder a little more about the correctness of the logic itself. Maybe, anyway; I can live in hope.
I first wrote github-to-omnifocus in Javascript. A year or so ago, I rewrote it in Go. After this positive experience with Rust, I’m tempted to use it as a next experiment…