References to Literals in Rust?!
One day messing around with Rust, I found that the following code is valid:
That’s assigning a variable to a reference to the literal
0 - how?! Why?! This absolutely shocked me. Just try doing this in C++ and you’ll see why:
The literal is a temporary - you can’t have a reference to that! String literals are lvalues in C++, but that’s a weird special case. That’s why you can assign it to a pointer like
const char *, but can’t get a
const int * from an integer literal.
Why is this shocking?
This may not seem that shocking to some. Literals are generally temporary and don’t really live anywhere in memory - they’re essentially hard coded constants in the program. A reference points to some place in memory. How do we point to something that doesn’t live in memory? Well, we can’t, and we don’t!
Rvalue Static Promotion
This concept in Rust is called rvalue static promotion. We can look at each part to see what that means:
Rvalue: Something that can only be on the right hand side of an assignment. For example, you can’t do
1 = x because the literal
1 is an rvalue.
Static: Something that is valid for the whole lifetime of the program.
So we promote the rvalue to a static value in order to take a reference to it. Looking at the program earlier, we can see this in action in Rust’s playground. We can see the MIR (one of the intermediate representations of Rust) is:
This is a little weird to look at if you’ve never seen MIR before, but the important part is the line
promoted in main: &i32 - that’s where we see the promoted variable! Then in the main program we assign with
_2 = const main::promoted;. So we lift the literal out to a static lifetime in order to return a reference, pretty neat.
Why did they do this?
I find this the interesting part. We can see a lot of the motivation for this in the feature:
The necessary changes in the compiler did already get implemented as part of codegen optimizations (emitting references-to or memcopies-from values in static memory instead of embedding them in the code).
It seems like it was just an easy thing to implement, so they did it. Their drawback is pretty interesting:
One more feature with seemingly ad-hoc rules to complicate the language…
I found this funny. Seems like they just thought “it’s easy enough, could be useful, why not?” So, they added a new feature to the Rust language. So many languages get by without this, but the Rust devs said, why not?
You can see this exact thing in action in Rust’s source code! At the time of writing, you can see this here:
The fourth parameter is a reference to the literal
Well, I don’t know how useful you’d say it is. But, it’s an interesting thing in a common compiler that not many languages have.