Learning Rust: Week 2

2023/04/30

This week of Rust was focused almost entirely on last week’s technique #4, open source contribution! I spent the week working on three issues.

Issue #1: Documentation

This issue dealt with documenting two specific commands which had been added to Helix as part of a recent update that introduced soft line wrapping. I proposed extending the documentation generator to automatically document all commands, but then it turned out that someone was already doing the same thing (but probably better than I would have), so I just manually updated the docs. A nice first issue, I didn’t have to write any code but ended up reading some in order to locate the docstrings, which can only be found in the source right now.

Issue #2: Restart Language Servers on Crash

I looked into this one a fair bit before deciding that it was too hard for me. They want language servers to restart when they crash, and trying to figure out how to make this happen in Rust was very, very valuable! I learned a ton about asynchronous stuff, which is essential to Rust’s design (Rust is supposed to be thread-safe by default). As best I can tell, it would require changing the LS registry from mapping language IDs to Arc<Client>s to Arc<RefCell<Client>>s, then somehow adding a routine to the error handling that would asynchronously use the RefCell to acquire a write-lock on the client, poll Client._process.id() to see if it died, and, if so, restarting the client.

Anyway, even if this wasn’t solved, it provided excellent experience navigating large codebases and doing async things in Rust.

Issue #3: Only Render Graphemes in Viewport

Helix doesn’t render lines that won’t show up in the viewport, but this issue suggests a speed improvement where you also don’t render columns which won’t show up, which can be helpful when dealing with long lines. Implementing this was easier and only took a few hours one morning, but as it turns out it wasn’t very helpful because it’s harder to Still a reasonably good experience and I learned about Rust’s very nice labelled loops feature, which lets you do things like:

'outer: loop {
	'inner: loop {
		// ...
		continue 'outer;
		break 'outer;
	}
}

Issue #4: Use Gitignore Outside of Git Repos

This issue ended up being very simple, to the point that the feature was something like 5 lines of code (mostly a testament to the Helix team’s ability to write awesome, extensible code). It represents the only code I was able to contribute to Helix this week!

Summary

I think this week highlights in a number of ways the upsides and downsides of doing open-source contribution to learn a programming language. Open source doesn’t mean low standards: people want your contributions to be good. This makes the learning curve incredibly steep - you’re reading, and expected to write, high-quality code in a language you aren’t very solid on yet. I probably spent around 15 hours working on and thinking about Helix this week, and I ended up submitting less than 20 lines to their codebase. However, the two coding issues that I didn’t end up submitting PRs for were extremely useful as a way to learn in “shadow mode,” where you try to implement features for yourself even if that doesn’t result in code getting committed to the project. There’s even a supervised version of this, where you look at past PRs, try to implement the feature for yourself, and then get the feedback of how someone did it in reality.

(As an aside, this might also be a sort of cool way to train LLMs to code. Apparently, a modern issue with this is that all publicly-available code has been exhausted. But has all historically available code been exhausted? There’s a huge potential dataset of changes to codebases and English-language descriptions and discussions of those changes.)

One last feature I’d like to mention with regards to open-source contribution: it’s fun and motivating! My code is in a project that thousands of people use! I don’t think I’d have spent 15 hours doing anything else from my original list, unless I had a small project that I really loved. This is a giant advantage.

Next Week

Next week I probably won’t do very much because I’m graduating with my master’s degree and my family will be in town! I may touch on #5 from last week’s list, Re-Implementing Standard Library Components.