Llogiq on stuff

From Tweet to Rust Feature

Today let’s look at how the sausage Rust language is made, on the case of a feature that I know a bit about, because I proposed and implemented it.

On May 19th, I tweeted:

I think @rustlang needs a better story on how to setup & use static immutable data, one thing C is notoriously good at.

The problem I observed here is that current Rust needs full types for static and const items (for those who don’t know, the former are simply pieces of data in the data area of the executable, whereas the latter are constant definitions whose value is copied everywhere they appear). This includes lifetimes, and just about the only lifetime available to those items is 'static, which is seven characters long and contributes substantially to rightwards drift in the code for those items, e.g.

static GREETING : &'static [&'static str] = &["Hello", "World"];

This is a simple string array reference, but the type took twenty-three characters to write. The lifetimes are the only ones that can be used there, so they convey no information. Defaulting the lifetimes to 'static gets us:

static GREETING : &[&str] = &["Hello", "World"];

This removes sixteen characters, and makes it easier to write and read.

The twitter discussion led me to write Rust Request for Comments (RFC) #1623 the next day. Just less than 100 lines, 612 words. Not much, but the feature is quite simple. I should note that this was not a literary masterpiece, just a plain and simple description of how I thought it should work, with a few examples, alternatives and drawbacks.

After some interesting and useful discussion surveying interesting interactions with other features, some of which aren’t even on the table yet, the RFC entered final comment period on August 12th and was accepted on August 22nd. I promptly set out to implement it under the awesome mentoring of Niko Matsakis.

The change would have taken only one line of code were it not for the fact that the feature needed to be unstable for now, which complicated matters a smidgen. Regardless, I did nothing particularly clever while implementing it (and even made some errors on first try), so I’m convinced even a relative Rust newbie could have done it.

Now why do I tell you this? Because I’m not some kind of super programmer. I’m just another person on the internet doing this in my spare free time. If I can do it, you can, too. Have an idea for an awesome Rust feature? Discuss it, and maybe you can write that next RFC and improve Rust for the rest of us.

PS.: If you want to try it you need a nightly rustc and add #![feature(static_in_const)] to the top of your crate.

PPS.: And I wasn’t even the first to talk about it. Redditor VadimVP beat me to it with an internals post – in 2014!