Rust is Hard

I’m not a Rust expert by any means, but I’ve spent enough time with the language that I can write reasonable code with a bit of effort (and a lot of DuckDuckGo). However, I’ve found that I often hesitate to use it for my personal projects.

Rust is a large language. The sheer number of features and their complexity introduce so much cognitive overhead while writing code that I can’t always muster up the mental energy to use it for things I’m simply tinkering around with after work. It’s just not possible for me to keep the whole thing in my head.

If I was writing Rust for my day job, it would be a different matter. Using it to build production software every day would let me internalize it to the point that it would become mostly automatic. It would let me cement difficult concepts in my brain so that I wouldn’t have to go looking for explanations every five minutes (lifetimes, anyone?)

Sadly I only get to use Rust for one-off side-projects, which means I spend maybe two hours a week using it. This gives me barely enough time to get my code into a working state, let alone dive into things like the Rustonomicon or even macros.

I love Rust, so I’m going to continue using it despite my issues with it. I’ll just have to lower my expectations of the level of proficiency I can hope to attain with the language.

PS: somebody please give me a Rust job 😭

Rust: Structuring and handling errors in 2020 (from Nick Groenen)

Everything I’ve built with Rust so far has been tiny in both scope and size, so writing out my error handling code by hand hasn’t been much of a hassle. However, I’ve been meaning to look into some of the popular error handling crates so I don’t have to write a ton of boilerplate every time I want to introduce a new type of error in my applications.

Nick Groenen covers two such crates in his blog post on error handling in Rust: https://nick.groenen.me/posts/rust-error-handling/

This reddit thread has further suggestions: https://www.reddit.com/r/rust/comments/gj8inf/rust_structuring_and_handling_errors_in_2020/

Week of 25 May, 2020

  • America is burning. India has been burning for months. Unable to contribute much to what’s going on, I’m educating myself. The only thing I really know how to do is read and write, so I’m reading. Maybe one day I will write.
  • I cooked a giant pot of creamy, buttery dal bukhara and I’m unreasonably proud of this achievement. I’d make a great house-husband.
  • Darkest Dungeon was free for the weekend, so I’ve spent the last two days gleefully getting my ass kicked by eldritch horrors. I’ve been wanting to play this game for months, and I’m happy to report that it lives up to the hype.
  • Tehanu is incredible. You can tell within the first few pages that Le Guin was an entirely different person by the time she got around to writing it.
  • Work has been frustrating. I’ve been spinning my wheels trying to build a thing that should be straightforward, but isn’t. It’s been making me not want to even look at my IDE. I’m close to a solution now, so hopefully this period of reduced productiveness will only last a few more days.
  • Charli still on repeat.

GoAccess

CloudFlare recently informed me that this website was getting thousands of hits every day, which is an unusual occurrence. I pulled up Google Analytics to figure out where all that traffic was coming from, only to be informed that I was getting three to four hundred daily visitors at most.

This felt a bit suspicious, so I dug into my Nginx logs to see if something was up. I pulled up the logs in Vim, but it was too hard to make sense of any of the raw data by reading it line-by-line. I needed something that could help me visualize my logs. I asked around and found a little tool called GoAccess.

GoAccess is an open-source log analyzer that can help you visualize your server logs in the terminal or use them to produce an HTML report. I installed it from the Ubuntu repositories and ran it:

goaccess /var/log/nginx/access.log

I was greeted with a dialog listing a bunch of popular log formats, asking me which one my file conformed to. After searching the Web for a bit, I figured out that Nginx uses something called the NCSA Combined Log Format for its messages. I picked that in the dialog and was on my way.

After a few minutes of looking at the aggregated data, I felt that analyzing just one file wasn’t telling me the entire story. I wondered if I could analyze all the logs produced by Nginx in the last month at once. After searching the Web a bit more, I found that I could use zcat to unzip the older logs and print them to stdout, and then pipe that into GoAccess. So I did this:

zcat -f /var/log/nginx/access.log* | goaccess --log-format=COMBINED

Turns out someone was trying to exploit my website by by sending malicious inputs to WordPress’s xmlrpc.php. From the access patterns, it looked more like a drive-by automated attack than a human trying to break in. Since I didn’t need any of the features enabled by the WordPress API, I blocked access to xmlrpc.php entirely:

location = /xmlrpc.php {
    deny all;
    access_log off;
    log_not_found off;
}

The downside to this is that I can’t post to my website from the WordPress app on my iPhone. But that’s not something I do often, so losing that feature is not a big deal.

After this incident, I also removed Google Analytics from my website. I’m fundamentally opposed to business models that are based on surveillance-based advertising, which is why I try to stay away from Google products as much as possible. My server logs give me enough data to judge how well my posts are doing, and with GoAccess I now have a reasonable way of querying and visualizing that data. That’s pretty much all I need.

The Expression Problem

While reading Crafting Interpreters, I learned about something called the expression problem. It’s a problem I’ve run into countless times, especially in larger projects, but I never knew it had an actual name. As usual, Eli Bendersky has an in-depth article about the problem and its solution in C++ and Clojure.

After reading Eli’s article, I wondered how the problem would manifest in Rust and what the solution would be. Turns out it’s not too complicated. Want to add a new type? Just add it and implement all the traits you care about. Want to add a new operation? Define a new trait and implement it on all the types you care about. This article goes into more detail on what that looks like.

Week of April 6, 2020

  • Spent a lot of time on calls with potential clients. Happy to report that I can sound very professional even when I’m not wearing pants.
  • Started reading Crafting Interpreters. With a lot more Rust under my belt than the last time I tried this, things are going better than I expected. I even managed to add reasonable (by my standards) error handling to my baby interpreter!
  • Started posting things on this blog again, trying to keep it fun and low-stakes. I want to have fun writing, and turns out Growing an Audience™ and Using Fully-Formed Sentences™ is the exact opposite of fun.
  • Played a lot of Radiant Historia on the 3DS. I’m loving the gameplay and story so far, but the combat needs to get more interesting soon.
  • Played a few hours of Into the Breach on the Switch. Holy. Fucking. Shit. This is easily one of the best designed video games I’ve ever played. I see myself pouring many many hours into this gem!
  • We got a new kitten just before pandemic, hoping that she’d become friends with our older cat. Did not go according to plan. It’s impossible to leave them in the same room together without one of them trying to murder the other every five minutes. We’re keeping them separate for now, hoping for at least an uncomfortable truce.
  • The first half of Gandhi: The Years that Changed the World was incredible, but now the book is getting repetitive. It has a lot of detail that I consider unnecessary. I’m somehow powering through the last 200 pages so I can pick up Ursula K. Le Guin’s Earthsea series.
  • Some albums I enjoyed this week were: Saint Cloud by Waxahatchee, 2017-2019 by Against All Logic, and Aarupa by Quinton Barnes.

People Sure Have Opinions About Programming Languages

tl;dr: everyone loves pattern matching.