The Rust team is happy to announce a new version of Rust, 1.37.0. Rust is a programming language that is empowering everyone to build reliable and efficient software.
If you have a previous version of Rust installed via rustup, getting Rust 1.37.0 is as easy as:
$ rustup update stable
If you don't have it already, you can get rustup
from the appropriate page on our website, and check out the detailed release notes for 1.37.0 on GitHub.
What's in 1.37.0 stable
The highlights of Rust 1.37.0 include referring to enum
variants through type
aliases, built-in cargo vendor
, unnamed const
items, profile-guided optimization, a default-run
key in Cargo, and #[repr(align(N))]
on enum
s. Read on for a few highlights, or see the detailed release notes for additional information.
enum
variants through type
aliases
Referring to With Rust 1.37.0, you can now refer to enum
variants through type aliases. For example:
type ByteOption = Option<u8>;
fn increment_or_zero(x: ByteOption) -> u8 {
match x {
ByteOption::Some(y) => y + 1,
ByteOption::None => 0,
}
}
In implementations, Self
acts like a type alias. So in Rust 1.37.0, you can also refer to enum
variants with Self::Variant
:
impl Coin {
fn value_in_cents(&self) -> u8 {
match self {
Self::Penny => 1,
Self::Nickel => 5,
Self::Dime => 10,
Self::Quarter => 25,
}
}
}
To be more exact, Rust now allows you to refer to enum
variants through "type-relative resolution", <MyType<..>>::Variant
. More details are available in the stabilization report.
Built-in Cargo support for vendored dependencies
After being available as a separate crate for years, the cargo vendor
command is now integrated directly into Cargo. The command fetches all your project's dependencies unpacking them into the vendor/
directory, and shows the configuration snippet required to use the vendored code during builds.
There are multiple cases where cargo vendor
is already used in production: the Rust compiler rustc
uses it to ship all its dependencies in release tarballs, and projects with monorepos use it to commit the dependencies' code in source control.
const
items for macros
Using unnamed You can now create unnamed const
items. Instead of giving your constant an explicit name, simply name it _
instead. For example, in the rustc
compiler we find:
/// Type size assertion where the first parameter
/// is a type and the second is the expected size.
#[macro_export]
macro_rules! static_assert_size {
($ty:ty, $size:expr) => {
const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
// ^ Note the underscore here.
}
}
static_assert_size!(Option<Box<String>>, 8); // 1.
static_assert_size!(usize, 8); // 2.
Notice the second static_assert_size!(..)
: thanks to the use of unnamed constants, you can define new items without naming conflicts. Previously you would have needed to write static_assert_size!(MY_DUMMY_IDENTIFIER, usize, 8);
. Instead, with Rust 1.37.0, it now becomes easier to create ergonomic and reusable declarative and procedural macros for static analysis purposes.
Profile-guided optimization
The rustc
compiler now comes with support for Profile-Guided Optimization (PGO) via the -C profile-generate
and -C profile-use
flags.
Profile-Guided Optimization allows the compiler to optimize code based on feedback from real workloads. It works by compiling the program to optimize in two steps:
- First, the program is built with instrumentation inserted by the compiler. This is done by passing the
-C profile-generate
flag torustc
. The instrumented program then needs to be run on sample data and will write the profiling data to a file. - Then, the program is built again, this time feeding the collected profiling data back into
rustc
by using the-C profile-use
flag. This build will make use of the collected data to allow the compiler to make better decisions about code placement, inlining, and other optimizations.
For more in-depth information on Profile-Guided Optimization, please refer to the corresponding chapter in the rustc book.
Choosing a default binary in Cargo projects
cargo run
is great for quickly testing CLI applications. When multiple binaries are present in the same package, you have to explicitly declare the name of the binary you want to run with the --bin
flag. This makes cargo run
not as ergonomic as we'd like, especially when a binary is called more often than the others.
Rust 1.37.0 addresses the issue by adding default-run
, a new key in Cargo.toml
. When the key is declared in the [package]
section, cargo run
will default to the chosen binary if the --bin
flag is not passed.
#[repr(align(N))]
on enum
s
The #[repr(align(N))]
attribute can be used to raise the alignment of a type definition. Previously, the attribute was only allowed on struct
s and union
s. With Rust 1.37.0, the attribute can now also be used on enum
definitions. For example, the following type Align16
would, as expected, report 16
as the alignment whereas the natural alignment without #[repr(align(16))]
would be 4
:
#[repr(align(16))]
enum Align16 {
Foo { foo: u32 },
Bar { bar: u32 },
}
The semantics of using #[repr(align(N))
on an enum
is the same as defining a wrapper struct AlignN<T>
with that alignment and then using AlignN<MyEnum>
:
#[repr(align(N))]
struct AlignN<T>(T);
Library changes
In Rust 1.37.0 there have been a number of standard library stabilizations:
BufReader::buffer
andBufWriter::buffer
Cell::from_mut
Cell::as_slice_of_cells
DoubleEndedIterator::nth_back
Option::xor
{i,u}{8,16,32,64,128,size}::reverse_bits
andWrapping::reverse_bits
slice::copy_within
Other changes
There are other changes in the Rust 1.37 release: check out what changed in Rust, Cargo, and Clippy.
Contributors to 1.37.0
Many people came together to create Rust 1.37.0. We couldn't have done it without all of you. Thanks!
New sponsors of Rust infrastructure
We'd like to thank two new sponsors of Rust's infrastructure who provided the resources needed to make Rust 1.37.0 happen: Amazon Web Services (AWS) and Microsoft Azure.
- AWS has provided hosting for release artifacts (compilers, libraries, tools, and source code), serving those artifacts to users through CloudFront, preventing regressions with Crater on EC2, and managing other Rust-related infrastructure hosted on AWS.
- Microsoft Azure has sponsored builders for Rust’s CI infrastructure, notably the extremely resource intensive rust-lang/rust repository.