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

Share
  • August 16, 2019

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;

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 with that alignment and then using AlignN.

#[repr(align(N))]
struct AlignN(T);

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

The post Rust 1.37.0 release: refer to enum variants through type aliases & more appeared first on JAXenter.

Source : JAXenter