Rust 1.37.0 stable release

What Rust 1.37.0 stable there?

Rust highlights 1.37.0 comprises typedef enum by reference, built cargo vendor, an un-named macro const, profile-guided optimization, the Cargo # default-run and the enumeration [repr (align ( N))].

Enum type referenced by the alias

In Rust 1.37.0, you can be referenced by enumeration type alias. E.g:

type ByteOption = Option<u8>;

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

To achieve it, Self acts as a type alias. In Rust 1.37.0, you can also use the Self: Variable referenced enumeration:

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

Rather, Rust now allowed to refer to enumerate variables "type-relative resolution", <MyType <.. >> :: Variant.

Built-in support for independent cargo dependencies

Cargo integrated cargo vendor command, the command gets all project dependencies, decompresses them to the vendor / directory, and demonstrated the use of codes distributed in the build process configuration snippet.

cargo vendor has been used in production over a variety of situations: Rust compiler rustc use it passed all its dependencies in the distribution tarball, whereas projects with Monorepos use it to submit the code in source control dependencies.

Unnamed use const macro

You can now create unnamed const. Compared to an explicit name to the constant, simply named _. For example, the compiler rustc:

/// 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.

Note that the second static_assert_size (..):! Due to the use of unnamed constants, you can define a new item without naming conflicts. Previously, you need to write static_assert_size! (MY_DUMMY_IDENTIFIER, usize, 8) ;. In Rust 1.37.0, it can more easily create ergonomic and reusable declarative process and macro static analysis purposes.

Optimization (profile guided Profile <br>-the Guided Optimization )

rustc compiler now provides support for the guide profile optimization (PGO) by -C profile-generate and -C profile-use flag.

Profile-Guided Optimization  allows the compiler to optimize code based on feedback from the actual workload. The way it works is the compiler, optimized in two steps:

  1. First, the program is inserted by the compiler tool build. This is achieved by -C profile-generate flag to rustc accomplished. Then, you run tests on the sample data, and analyzes the data written to the file
  2. After that, the rebuilding program, this time using the -C profile-use markers to analyze the data collected will be fed back rustc. This construct will use the data collected to be placed so that the compiler code, and other optimization inlining make better decisions

In the Cargo project, select the default binary file

cargo run a quick test for CLI applications are possible when multiple binary file name that appears in the same time a package, you must use a binary file --bin flag explicitly declared to be run. This makes cargo run not as ergonomically what we want, especially when a binary file is called more frequently than any other time.

Rust 1.37.0 to solve this problem by adding a default-run, this is Cargo.toml in a new key, when the key in the [Package] in the Declarations section, if --bin flag is not passed, cargo run will default the selected binary files.

# [Repr (align (N))] on enumeration

# [Repr (align (N))] can be used to improve the alignment property of the type defined. Previously, this property is only allowed to be used in the struct and union. In Rust 1.37.0, this property can now also be used to define enumeration. For example, the following types Align16 as expected, reported in alignment 16, without using # [repr (align (16))] will be aligned with 4:

#[repr(align(16))]
enum Align16 {
    Foo { foo: u32 },
    Bar { bar: u32 },
}

In enumerating # [repr (align (N))] using the semantic structure of the package body Qiding Yi AlignN <T>, as then AlignN <MyEnum>:

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

In Rust 1.37.0, some standard libraries stabilization:

Gets Rust 1.37.0 as follows:

$ rustup update stable

Or visit the official website to obtain:

https://www.rust-lang.org/install.html

For details, see the release notes

Guess you like

Origin www.oschina.net/news/109137/rust-1-37-0-released