Skip to content

Commit 1b6d03e

Browse files
committed
various improvements to the 1.46 blog post
1 parent bfd17f5 commit 1b6d03e

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

posts/2020-08-27-Rust-1.46.0.md

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,54 @@ appropriate page on our website, and check out the [detailed release notes for
2525

2626
## What's in 1.46.0 stable
2727

28-
This release is on the smaller side, with a number of improvements to `const
29-
fn`, two new standard library APIs, and one feature useful for library
30-
authors. See the [detailed release notes][notes] to learn about other changes
31-
not covered by this post.
28+
This release enables quite a lot of new things to appear in `const fn`, two
29+
new standard library APIs, and one feature useful for library authors. See
30+
the [detailed release notes][notes] to learn about other changes not covered
31+
by this post.
32+
33+
### `const fn` improvements
34+
35+
There are [several core language features] you can now use in a `const fn`:
36+
37+
* `if`, `if let`, and `match`
38+
* `while`, `while let`, and `loop`
39+
* the `&&` and `||` operators
40+
41+
You can also [cast to a slice][cast-to-slice]:
42+
43+
```rust
44+
const fn foo() {
45+
let x = [1, 2, 3, 4, 5];
46+
47+
// cast the array to a slice
48+
let y: &[_] = &x;
49+
}
50+
```
51+
52+
While these features may not feel *new*, given that you could use them all
53+
outside of `const fn`, they add a lot of compile-time computation power! As
54+
an example, the [`const-sha1` crate][sha1] can let you compute SHA-1 hashes
55+
at compile time. This led to a [40x performance improvement][const-perf] in
56+
Microsoft's WinRT bindings for Rust.
57+
58+
[several core language features]: https://github.com/rust-lang/rust/pull/72437/
59+
[cast-to-slice]: https://github.com/rust-lang/rust/pull/73862/
60+
[sha1]: https://github.com/rylev/const-sha1
61+
[const-perf]: https://github.com/microsoft/winrt-rs/pull/279#issuecomment-668436700
62+
3263

3364
### `#[track_caller]`
3465

35-
Back in March, the release of Rust 1.42 introduced [better error messages when `unwrap()` and related functions would panic][better-errors]. At the time, we mentioned that the way
66+
Back in March, the release of Rust 1.42 introduced [better error messages when `unwrap` and related functions would panic][better-errors]. At the time, we mentioned that the way
3667
this was implemented was not yet stable. Rust 1.46 stabilizes this feature.
3768

3869
[better-errors]: https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#useful-line-numbers-in-option-and-result-panic-messages
3970

40-
This attribute is called `#[track_caller]`, which was originally proposed
41-
in [RFC 2091][rfc-2091] way back in July of 2017! If you're writing a function
42-
like `unwrap()` that may panic but should not itself appear in the panic stacktrace, you can put this
43-
annotation on your functions, and the default panic formatter will use it to
44-
print its error message. For example, here is `unwrap` previously:
71+
This attribute is called `#[track_caller]`, which was originally proposed in
72+
[RFC 2091][rfc-2091] way back in July of 2017! If you're writing a function
73+
like `unwrap` that may panic, you can put this annotation on your functions,
74+
and the default panic formatter will use its caller as the location in its
75+
error message. For example, here is `unwrap` previously:
4576

4677
```rust
4778
pub fn unwrap(self) -> T {
@@ -72,28 +103,6 @@ on `std::panic::Location` to get access to this information.
72103
[rfc-2091]: https://github.com/rust-lang/rfcs/pull/2091
73104
[caller]: https://doc.rust-lang.org/stable/std/panic/struct.Location.html#method.caller
74105

75-
### `const fn` improvements
76-
77-
There are [several core language features] you can now use in a `const fn`:
78-
79-
* `if`, `if let`, and `match`
80-
* `while`, `while let`, and `loop`
81-
* the `&&` and `||` operators
82-
83-
You can also [cast to a slice][cast-to-slice]:
84-
85-
```rust
86-
const fn foo() {
87-
let x = [1, 2, 3, 4, 5];
88-
89-
// cast the array to a slice
90-
let y: &[_] = &x;
91-
}
92-
```
93-
94-
[several core language features]: https://github.com/rust-lang/rust/pull/72437/
95-
[cast-to-slice]: https://github.com/rust-lang/rust/pull/73862/
96-
97106
### Library changes
98107

99108
Keeping with the theme of `const fn` improvements, [`std::mem::forget` is now

0 commit comments

Comments
 (0)