Skip to content

Commit 1b53c36

Browse files
Merge pull request #789 from XAMPPRocky/rust-1.51.0
Add blog post for 1.51.0
2 parents 2dab7d8 + 55559ff commit 1b53c36

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

posts/2021-03-25-Rust-1.51.0.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
layout: post
3+
title: "Announcing Rust 1.51.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.51.0. Rust is a
9+
programming language that is empowering everyone to build reliable and
10+
efficient software.
11+
12+
If you have a previous version of Rust installed via rustup, getting Rust
13+
1.51.0 is as easy as:
14+
15+
```console
16+
rustup update stable
17+
```
18+
19+
If you don't have it already, you can [get `rustup`][install]
20+
from the appropriate page on our website, and check out the
21+
[detailed release notes for 1.51.0][notes] on GitHub.
22+
23+
[install]: https://www.rust-lang.org/install.html
24+
[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1510-2021-03-25
25+
26+
## What's in 1.51.0 stable
27+
This release represents one of the largest additions to the Rust language and Cargo in quite a while, stabilizing an MVP of const generics and a new feature resolver for Cargo. Let's dive right into it!
28+
29+
30+
### Const Generics MVP
31+
Before this release, Rust allowed you to haves your types be parameterized over lifetimes or types. For example if we wanted to have a `struct` that is generic over the element type of an array, we'd write the following:
32+
33+
```rust
34+
struct FixedArray<T> {
35+
// ^^^ Type generic definition
36+
list: [T; 32]
37+
// ^ Where we're using it.
38+
}
39+
```
40+
41+
If we then use `FixedArray<u8>`, the compiler will make a monomorphic version of `FixedArray` that looks like:
42+
43+
```rust
44+
struct FixedArray<u8> {
45+
list: [u8; 32]
46+
}
47+
```
48+
49+
This is a powerful feature that allows you to write reusable code with no runtime overhead. However, until this release it hasn't been possible to easily be generic over the *values* of those types. This was most notable in arrays which include their length in their type definition (`[T; N]`), which previously you could not be generic over. Now with 1.51.0 you can write code that is generic over the values of any integer, `bool`, or `char` type! (Using `struct` or `enum` values is still unstable.)
50+
51+
This change now lets us have our own array struct that's generic over its type *and* its length. Let's look at an example definition, and how it can be used.
52+
53+
```rust
54+
struct Array<T, const LENGTH: usize> {
55+
// ^^^^^^^^^^^^^^^^^^^ Const generic definition.
56+
list: [T; LENGTH]
57+
// ^^^^^^ We use it here.
58+
}
59+
```
60+
61+
Now if we then used `Array<u8, 32>`, the compiler will make a monomorphic version of `Array` that looks like:
62+
63+
```rust
64+
struct Array<u8, 32> {
65+
list: [u8; 32]
66+
}
67+
```
68+
69+
Const generics adds an important new tool for library designers in creating new, powerful compile-time safe APIs. If you'd like to learn more about const generics you can also check out the ["Const Generics MVP Hits Beta"][const-generics-blog] blog post for more information about the feature and its current restrictions. We can't wait to see what new libraries and APIs you create!
70+
71+
[const-generics-blog]: https://blog.rust-lang.org/2021/02/26/const-generics-mvp-beta.html
72+
73+
### `array::IntoIter` Stabilisation
74+
75+
As part of const generics stabilising, we're also stabilising a new API that uses it, `std::array::IntoIter`. `IntoIter` allows you to create a by value iterator over any array. Previously there wasn't a convenient way to iterate over owned values of an array, only references to them.
76+
77+
```rust
78+
fn main() {
79+
let array = [1, 2, 3, 4, 5];
80+
81+
// Previously
82+
for item in array.iter().copied() {
83+
println!("{}", item);
84+
}
85+
86+
// Now
87+
for item in std::array::IntoIter::new(array) {
88+
println!("{}", item);
89+
}
90+
}
91+
```
92+
93+
Note that this is added as a separate method instead of `.into_iter()` on arrays, as that currently introduces some amount of breakage; currently `.into_iter()` refers to the slice by-reference iterator. We're exploring ways to make this more ergonomic in the future.
94+
95+
### Cargo's New Feature Resolver
96+
97+
Dependency management is a hard problem, and one of the hardest parts of it is just picking what *version* of a dependency to use when it's depended on by two different packages. This doesn't just include its version number, but also what features are or aren't enabled for the package. Cargo's default behaviour is to merge features for a single package when it's referred to multiple times in the dependency graph.
98+
99+
For example, let's say you had a dependency called `foo` with features A and B, which was being used by packages `bar` and `baz`, but `bar` depends on `foo+A` and `baz` depends on `foo+B`. Cargo will merge both of those features and compile `foo` as `foo+AB`. This has a benefit that you only have to compile `foo` once, and then it can reused for both `foo` and `bar`.
100+
101+
However, this also comes with a downside. What if a feature enabled in a build-dependency is not compatible with the target you are building for?
102+
103+
A common example of this in the ecosystem is the optional `std` feature included in many `#![no_std]` crates, that allows crates to provide added functionality when `std` is available. Now imagine you want to use the `#![no_std]` version of `foo` in your `#![no_std]` binary, and use the `foo` at build time in your `build.rs`. If your build time dependency depends on `foo+std`, your binary now also depends on `foo+std`, which means it will no longer compile because `std` is not available for your target platform.
104+
105+
This has been a long-standing issue in cargo, and with this release there's a new `resolver` option in your `Cargo.toml`, where you can set `resolver="2"` to tell cargo to try a new approach to resolving features. You can check out [RFC 2957] for a detailed description of the behaviour, which can be summarised as follows.
106+
107+
- **Dev dependencies** — When a package is shared as a normal dependency and a dev-dependency, the dev-dependency features are only enabled if the current build is including dev-dependencies.
108+
- **Host Dependencies** — When a package is shared as a normal dependency and a build-dependency or proc-macro, the features for the normal dependency are kept independent of the build-dependency or proc-macro.
109+
- **Target dependencies** — When a package appears multiple times in the build graph, and one of those instances is a target-specific dependency, then the features of the target-specific dependency are only enabled if the target is currently being built.
110+
111+
While this can lead to some crates compiling more than once, this should provide a much more intuitive development experience when using features with cargo. If you'd like to know more, you can also read the ["Feature Resolver"][feature-resolver@2.0] section in the Cargo Book for more information. We'd like to thank the cargo team and everyone involved for all their hard work in designing and implementing the new resolver!
112+
113+
```toml
114+
[package]
115+
resolver = "2"
116+
# Or if you're using a workspace
117+
[workspace]
118+
resolver = "2"
119+
```
120+
121+
[rfc 2957]: https://rust-lang.github.io/rfcs/2957-cargo-features2.html
122+
[feature-resolver@2.0]: https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-resolver-version-2
123+
124+
### Splitting Debug Information
125+
While not often highlighted in the release, the Rust teams are constantly working on improving Rust's compile times, and this release marks one of the largest improvements in a long time for Rust on macOS. Debug information maps the binary code back to your source code, so that the program can give you more information about what went wrong at runtime. In macOS, debug info was previously collected into a single `.dSYM` folder using a tool called `dsymutil`, which can take some time and use up quite a bit of disk space.
126+
127+
Collecting all of the debuginfo into this directory helps in finding it at runtime, particularly if the binary is being moved. However, it does have the drawback that even when you make a small change to your program, `dsymutil` will need to run over the entire final binary to produce the final `.dSYM` folder. This can sometimes add a lot to the build time, especially for larger projects, as all dependencies always get recollected, but this has been a necessary step as without it Rust's standard library didn't know how to load the debug info on macOS.
128+
129+
Recently, Rust backtraces switched to using a different backend which supports loading debuginfo without needing to run `dsymutil`, and we've stabilized support for skipping the `dsymutil` run. This can significantly speed up builds that include debuginfo and significantly reduce the amount of disk space used. We haven't run extensive benchmarks, but have seen a lot of reports of people's builds being a lot faster on macOS with this behavior.
130+
131+
You can enable this new behaviour by setting the `-Csplit-debuginfo=unpacked` flag when running `rustc`, or by setting the [`split-debuginfo`] `[profile]` option to `unpacked` in Cargo. The "unpacked" option instructs rustc to leave the .o object files in the build output directory instead of deleting them, and skips the step of running dsymutil. Rust's backtrace support is smart enough to know how to find these .o files. Tools such as lldb also know how to do this. This should work as long as you don't need to move the binary to a different location while retaining the debug information.
132+
133+
```toml
134+
[profile.dev]
135+
split-debuginfo = "unpacked"
136+
```
137+
138+
[`split-debuginfo`]: https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#split-debuginfo
139+
140+
### Stabilized APIs
141+
142+
In total, this release saw the stabilisation of 18 new methods for various types like `slice` and `Peekable`. One notable addition is the stabilisation of `ptr::addr_of!` and `ptr::addr_of_mut!`, which allow you to create raw pointers to unaligned fields. Previously this wasn't possible because Rust requires `&/&mut` to be aligned and point to initialized data, and `&addr as *const _` would then cause undefined behaviour as `&addr` needs to be aligned. These two macros now let you safely create unaligned pointers.
143+
144+
```rust
145+
use std::ptr;
146+
147+
#[repr(packed)]
148+
struct Packed {
149+
f1: u8,
150+
f2: u16,
151+
}
152+
153+
let packed = Packed { f1: 1, f2: 2 };
154+
// `&packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
155+
let raw_f2 = ptr::addr_of!(packed.f2);
156+
assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);
157+
```
158+
159+
The following methods were stabilised.
160+
161+
- [`Arc::decrement_strong_count`]
162+
- [`Arc::increment_strong_count`]
163+
- [`Once::call_once_force`]
164+
- [`Peekable::next_if_eq`]
165+
- [`Peekable::next_if`]
166+
- [`Seek::stream_position`]
167+
- [`array::IntoIter`]
168+
- [`panic::panic_any`]
169+
- [`ptr::addr_of!`]
170+
- [`ptr::addr_of_mut!`]
171+
- [`slice::fill_with`]
172+
- [`slice::split_inclusive_mut`]
173+
- [`slice::split_inclusive`]
174+
- [`slice::strip_prefix`]
175+
- [`slice::strip_suffix`]
176+
- [`str::split_inclusive`]
177+
- [`sync::OnceState`]
178+
- [`task::Wake`]
179+
180+
[`Once::call_once_force`]: https://doc.rust-lang.org/stable/std/sync/struct.Once.html#method.call_once_force
181+
[`sync::OnceState`]: https://doc.rust-lang.org/stable/std/sync/struct.OnceState.html
182+
[`panic::panic_any`]: https://doc.rust-lang.org/stable/std/panic/fn.panic_any.html
183+
[`slice::strip_prefix`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.strip_prefix
184+
[`slice::strip_suffix`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.strip_prefix
185+
[`Arc::increment_strong_count`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html#method.increment_strong_count
186+
[`Arc::decrement_strong_count`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html#method.decrement_strong_count
187+
[`slice::fill_with`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.fill_with
188+
[`ptr::addr_of!`]: https://doc.rust-lang.org/stable/std/ptr/macro.addr_of.html
189+
[`ptr::addr_of_mut!`]: https://doc.rust-lang.org/stable/std/ptr/macro.addr_of_mut.html
190+
[`array::IntoIter`]: https://doc.rust-lang.org/stable/std/array/struct.IntoIter.html
191+
[`slice::split_inclusive`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.split_inclusive
192+
[`slice::split_inclusive_mut`]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.split_inclusive_mut
193+
[`str::split_inclusive`]: https://doc.rust-lang.org/stable/std/primitive.str.html#method.split_inclusive
194+
[`task::Wake`]: https://doc.rust-lang.org/stable/std/task/trait.Wake.html
195+
[`Seek::stream_position`]: https://doc.rust-lang.org/stable/std/io/trait.Seek.html#method.stream_position
196+
[`Peekable::next_if`]: https://doc.rust-lang.org/stable/std/iter/struct.Peekable.html#method.next_if
197+
[`Peekable::next_if_eq`]: https://doc.rust-lang.org/stable/std/iter/struct.Peekable.html#method.next_if_eq
198+
199+
### Other changes
200+
201+
There are other changes in the Rust 1.51.0 release: check out what changed in [Rust](https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1510-2021-03-25), [Cargo](https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-151-2021-03-25), and [Clippy](https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-151).
202+
203+
### Contributors to 1.51.0
204+
Many people came together to create Rust 1.51.0. We couldn't have done it without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.51.0/)

0 commit comments

Comments
 (0)