|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Announcing Rust 1.37.0" |
| 4 | +author: The Rust Release Team |
| 5 | +release: true |
| 6 | +--- |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +If you have a previous version of Rust installed via rustup, getting Rust 1.37.0 is as easy as: |
| 11 | + |
| 12 | +```console |
| 13 | +$ rustup update stable |
| 14 | +``` |
| 15 | + |
| 16 | +If you don't have it already, you can [get `rustup`][install] from the appropriate page on our website, and check out the [detailed release notes for 1.37.0][notes] on GitHub. |
| 17 | + |
| 18 | +[install]: https://www.rust-lang.org/install.html |
| 19 | +[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1370-2019-08-15 |
| 20 | + |
| 21 | +## What's in 1.37.0 stable |
| 22 | + |
| 23 | +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][notes] for additional information. |
| 24 | + |
| 25 | +### Referring to `enum` variants through `type` aliases |
| 26 | + |
| 27 | +With Rust 1.37.0, you can now refer to `enum` variants through type aliases. For example: |
| 28 | + |
| 29 | +```rust |
| 30 | +type ByteOption = Option<u8>; |
| 31 | + |
| 32 | +fn increment_or_zero(x: ByteOption) -> u8 { |
| 33 | + match x { |
| 34 | + ByteOption::Some(y) => y + 1, |
| 35 | + ByteOption::None => 0, |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +In implementations, `Self` acts like a type alias. So in Rust 1.37.0, you can also refer to `enum` variants with `Self::Variant`: |
| 41 | + |
| 42 | +```rust |
| 43 | +impl Coin { |
| 44 | + fn value_in_cents(&self) -> u8 { |
| 45 | + match self { |
| 46 | + Self::Penny => 1, |
| 47 | + Self::Nickel => 5, |
| 48 | + Self::Dime => 10, |
| 49 | + Self::Quarter => 25, |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +[type_rel_report]: https://github.com/rust-lang/rust/pull/61682/#issuecomment-502472847 |
| 56 | + |
| 57 | +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][type_rel_report]. |
| 58 | + |
| 59 | +### Built-in Cargo support for vendored dependencies |
| 60 | + |
| 61 | +[vendor-crate]: https://crates.io/crates/cargo-vendor |
| 62 | + |
| 63 | +After being available [as a separate crate][vendor-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. |
| 64 | + |
| 65 | +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. |
| 66 | + |
| 67 | +### Using unnamed `const` items for macros |
| 68 | + |
| 69 | +[unnamed_const_pr]: https://github.com/rust-lang/rust/pull/61347/ |
| 70 | + |
| 71 | +You can now create [unnamed `const` items][unnamed_const_pr]. Instead of giving your constant an explicit name, simply name it `_` instead. For example, in the `rustc` compiler we find: |
| 72 | + |
| 73 | +```rust |
| 74 | +/// Type size assertion where the first parameter |
| 75 | +/// is a type and the second is the expected size. |
| 76 | +#[macro_export] |
| 77 | +macro_rules! static_assert_size { |
| 78 | + ($ty:ty, $size:expr) => { |
| 79 | + const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()]; |
| 80 | + // ^ Note the underscore here. |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +static_assert_size!(Option<Box<String>>, 8); // 1. |
| 85 | +static_assert_size!(usize, 8); // 2. |
| 86 | +``` |
| 87 | + |
| 88 | +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. |
| 89 | + |
| 90 | +### Profile-guided optimization |
| 91 | + |
| 92 | +[rustc_book_pgo]: https://doc.rust-lang.org/rustc/profile-guided-optimization.html |
| 93 | +[pgo_pr]: https://github.com/rust-lang/rust/pull/61268/ |
| 94 | +[pgo_wiki]: https://en.wikipedia.org/wiki/Profile-guided_optimization |
| 95 | + |
| 96 | +The `rustc` compiler now comes with [support for Profile-Guided Optimization (PGO)][pgo_pr] via the `-C profile-generate` and `-C profile-use` flags. |
| 97 | + |
| 98 | +[Profile-Guided Optimization][pgo_wiki] allows the compiler to optimize code based on feedback from real workloads. It works by compiling the program to optimize in two steps: |
| 99 | + |
| 100 | +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. |
| 101 | +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. |
| 102 | + |
| 103 | +For more in-depth information on Profile-Guided Optimization, please refer to the corresponding [chapter in the rustc book][rustc_book_pgo]. |
| 104 | + |
| 105 | +### Choosing a default binary in Cargo projects |
| 106 | + |
| 107 | +[`default-run`]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-default-run-field |
| 108 | +[`cargo run`]: https://doc.rust-lang.org/cargo/commands/cargo-run.html |
| 109 | + |
| 110 | +[`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. |
| 111 | + |
| 112 | +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. |
| 113 | + |
| 114 | +### `#[repr(align(N))]` on `enum`s |
| 115 | + |
| 116 | +[enum_align_pr]: https://github.com/rust-lang/rust/pull/61229 |
| 117 | +[ref_align_mod]: https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers |
| 118 | +[ref_align_explain]: https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment |
| 119 | + |
| 120 | +[The `#[repr(align(N))]` attribute][ref_align_mod] can be used to raise the [alignment][ref_align_explain] 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][enum_align_pr]. 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`: |
| 121 | + |
| 122 | +```rust |
| 123 | +#[repr(align(16))] |
| 124 | +enum Align16 { |
| 125 | + Foo { foo: u32 }, |
| 126 | + Bar { bar: u32 }, |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +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>`: |
| 131 | + |
| 132 | +```rust |
| 133 | +#[repr(align(N))] |
| 134 | +struct AlignN<T>(T); |
| 135 | +``` |
| 136 | + |
| 137 | +### Library changes |
| 138 | + |
| 139 | +[`BufReader::buffer`]: https://doc.rust-lang.org/std/io/struct.BufReader.html#method.buffer |
| 140 | +[`BufWriter::buffer`]: https://doc.rust-lang.org/std/io/struct.BufWriter.html#method.buffer |
| 141 | +[`Cell::from_mut`]: https://doc.rust-lang.org/std/cell/struct.Cell.html#method.from_mut |
| 142 | +[`Cell::as_slice_of_cells`]: https://doc.rust-lang.org/std/cell/struct.Cell.html#method.as_slice_of_cells |
| 143 | +[`DoubleEndedIterator::nth_back`]: https://doc.rust-lang.org/std/iter/trait.DoubleEndedIterator.html#method.nth_back |
| 144 | +[`Option::xor`]: https://doc.rust-lang.org/std/option/enum.Option.html#method.xor |
| 145 | +[`Wrapping::reverse_bits`]: https://doc.rust-lang.org/std/num/struct.Wrapping.html#method.reverse_bits |
| 146 | +[`{i,u}{8,16,64,128,size}::reverse_bits`]: https://doc.rust-lang.org/std/primitive.u8.html#method.reverse_bits |
| 147 | +[`slice::copy_within`]: https://doc.rust-lang.org/std/primitive.slice.html#method.copy_within |
| 148 | + |
| 149 | +In Rust 1.37.0 there have been a number of standard library stabilizations: |
| 150 | + |
| 151 | +- [`BufReader::buffer`] and [`BufWriter::buffer`] |
| 152 | +- [`Cell::from_mut`] |
| 153 | +- [`Cell::as_slice_of_cells`] |
| 154 | +- [`DoubleEndedIterator::nth_back`] |
| 155 | +- [`Option::xor`] |
| 156 | +- [`{i,u}{8,16,64,128,size}::reverse_bits`] and [`Wrapping::reverse_bits`] |
| 157 | +- [`slice::copy_within`] |
| 158 | + |
| 159 | +### Other changes |
| 160 | + |
| 161 | +[relnotes-cargo]: https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-137-2019-08-15 |
| 162 | +[relnotes-clippy]: https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-137 |
| 163 | + |
| 164 | +There are other changes in the Rust 1.37 release: check out what changed in [Rust][notes], [Cargo][relnotes-cargo], and [Clippy][relnotes-clippy]. |
| 165 | + |
| 166 | +## Contributors to 1.37.0 |
| 167 | + |
| 168 | +Many people came together to create Rust 1.37.0. We couldn't have done it |
| 169 | +without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.37.0/) |
| 170 | + |
| 171 | +## New sponsors of Rust infrastructure |
| 172 | + |
| 173 | +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. |
| 174 | + |
| 175 | +- 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. |
| 176 | +- Microsoft Azure has sponsored builders for Rust’s CI infrastructure, notably the extremely resource intensive rust-lang/rust repository. |
0 commit comments