days
-1
-6
hours
-1
-4
minutes
-5
-1
seconds
-4
-2
search
Updating Rust

Rust 1.37.0 release: refer to enum variants through type aliases & more

Chris Stewart
rust
© Shutterstock / Voyagerix

Rust is still growing in popularity, climbing up the TIOBE and Redmonk rankings this year. Version 1.37.0’s highlights include being able to refer 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 enums. Let’s take a closer look at the details.

Rust-lang version 1.37.0 arrived on August 15th, 2019 – and it brings with it quite a lot of changes. Let’s get straight to it, shall we?

Referring to enum variants through type aliases

It’s now possible to refer to enum variants through type aliases. The Rust blog offers the following example of this in action:

type ByteOption = Option<u8>;

fn increment_or_zero(x: ByteOption) -> u8 {
    match x {
        ByteOption::Some(y) => y + 1,
        ByteOption::None => 0,
    }
}

Furthermore, since Self acts like a type alias, it is now also possible to refer to enum variants with Self::Variant as well – here’s another example from the Rust blog:

impl Coin {
    fn value_in_cents(&self) -> u8 {
        match self {
            Self::Penny => 1,
            Self::Nickel => 5,
            Self::Dime => 10,
            Self::Quarter => 25,
        }
    }
}

Built-in Cargo support for vendored dependencies

The cargo vendor command is now integrated directly into Cargo, where previously it was only available as a separate crate. Typing this command will now collect and unpack all your project’s dependencies into the vendor/ directory. It will also show 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.

Using unnamed const items for macros

With 1.37.0 it’s now possible to use _ as an identifier for constants. As such, something like const _: u32 = 5; is now an option.

Profile-guided optimization

Profile-Guided Optimization (PGO) can now be enabled in the rustc compiler using the -C profile-generate and -C profile-use flags.

PGO enables the compiler to use feedback from real workloads to optimize code. It happens in two steps:

  1. First, the program is built with instrumentation inserted by the compiler. This is done by passing the -C profile-generate flag to rustc. The instrumented program then needs to be run on sample data and will write the profiling data to a file.
  2. 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.

SEE ALSO: Using open source in your enterprise? What to look out for

Choosing a default binary in Cargo projects

A new key has been added in Cargo.toml, default-run. When the key is declared in the [package] section, cargo run will default to the defined binary if the --bin flag isn’t passed.

#[repr(align(N))] on enums

The #[repr(align(N))] attribute can now be used to raise the alignment of a type definition.

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);

For more information read the Rust 1.37.0 blog announcement, or check out the release notes on GitHub.

Author
Chris Stewart
Chris Stewart is an Online Editor for JAXenter.com. He studied French at Somerville College, Oxford before moving to Germany in 2011. He speaks too many languages, writes a blog, and dabbles in card tricks.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of