02 February 2017
Today marks the release of Rust 1.15. I have been eagerly awaiting this specific release for a while, because it comes with procedural macros and custom derives. You should be excited too, because this release means a lot for production Rust without having to use nightly releases.
In the past, Rust has allowed you to derive certain pre-defined traits
for your structs and enums. Examples include Copy
, Clone
, Eq
, PartialEq
and Debug
.
Imagine that you want to create an enum and you want to check if
instances of the enum are equal. Here's what you'd do:
#[derive(Eq, PartialEq)]
enum Fruit {
Apple,
Orange,
}
The compiler would generate an implementation of the Eq
and PartialEq
traits for you. Easy and clean.
However, before today, stable Rust derive was limited to the pre-defined traits. Useful crates like Serde and Diesel provided custom derives, but these were only available on nightly.
Not anymore! Since today, if you update rust (rustup update stable
),
you can use custom derives on stable! That's right. You can now use
Serde and Diesel on stable.
I won't go into details on how to actually write procedural macros in this particular blog post, but just show you why this is really great. Previously, if you wanted to use Serde on stable with custom derives, you would have to jump through all kinds of hoops to get your code working.
Now all you have to do is
#[derive(Serialize, Deserialize)]
enum Fruit {
Apple,
Orange,
}
and your enum will be able to serialize to JSON and other formats.
Equally awesome is that Diesel now works on stable too. Imagine having a type safe ORM library. Imagine not having to write SQL statements. Imagine it only taking a few lines of code.
The greatest thing however, is that this means that Rust just gained a lot of bonus points as a production ready language. Diesel and Serde are two important libraries for writing applications in Rust. Now that they no longer require nightly to use properly, a major roadblock for stable Rust application development has been removed.
And that is why you should be excited about Rust 1.15.