-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
various improvements to the 1.46 blog post
- Loading branch information
1 parent
bfd17f5
commit 1b6d03e
Showing
1 changed file
with
41 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,23 +25,54 @@ appropriate page on our website, and check out the [detailed release notes for | |
|
||
## What's in 1.46.0 stable | ||
|
||
This release is on the smaller side, with a number of improvements to `const | ||
fn`, two new standard library APIs, and one feature useful for library | ||
authors. See the [detailed release notes][notes] to learn about other changes | ||
not covered by this post. | ||
This release enables quite a lot of new things to appear in `const fn`, two | ||
new standard library APIs, and one feature useful for library authors. See | ||
the [detailed release notes][notes] to learn about other changes not covered | ||
by this post. | ||
|
||
### `const fn` improvements | ||
|
||
There are [several core language features] you can now use in a `const fn`: | ||
|
||
* `if`, `if let`, and `match` | ||
* `while`, `while let`, and `loop` | ||
* the `&&` and `||` operators | ||
|
||
You can also [cast to a slice][cast-to-slice]: | ||
|
||
```rust | ||
const fn foo() { | ||
let x = [1, 2, 3, 4, 5]; | ||
|
||
// cast the array to a slice | ||
let y: &[_] = &x; | ||
} | ||
``` | ||
|
||
While these features may not feel *new*, given that you could use them all | ||
outside of `const fn`, they add a lot of compile-time computation power! As | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
RalfJung
Member
|
||
an example, the [`const-sha1` crate][sha1] can let you compute SHA-1 hashes | ||
at compile time. This led to a [40x performance improvement][const-perf] in | ||
Microsoft's WinRT bindings for Rust. | ||
|
||
[several core language features]: https://github.com/rust-lang/rust/pull/72437/ | ||
[cast-to-slice]: https://github.com/rust-lang/rust/pull/73862/ | ||
[sha1]: https://github.com/rylev/const-sha1 | ||
[const-perf]: https://github.com/microsoft/winrt-rs/pull/279#issuecomment-668436700 | ||
|
||
|
||
### `#[track_caller]` | ||
|
||
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 | ||
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 | ||
this was implemented was not yet stable. Rust 1.46 stabilizes this feature. | ||
|
||
[better-errors]: https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#useful-line-numbers-in-option-and-result-panic-messages | ||
|
||
This attribute is called `#[track_caller]`, which was originally proposed | ||
in [RFC 2091][rfc-2091] way back in July of 2017! If you're writing a function | ||
like `unwrap()` that may panic but should not itself appear in the panic stacktrace, you can put this | ||
annotation on your functions, and the default panic formatter will use it to | ||
print its error message. For example, here is `unwrap` previously: | ||
This attribute is called `#[track_caller]`, which was originally proposed in | ||
[RFC 2091][rfc-2091] way back in July of 2017! If you're writing a function | ||
like `unwrap` that may panic, you can put this annotation on your functions, | ||
and the default panic formatter will use its caller as the location in its | ||
error message. For example, here is `unwrap` previously: | ||
|
||
```rust | ||
pub fn unwrap(self) -> T { | ||
|
@@ -72,28 +103,6 @@ on `std::panic::Location` to get access to this information. | |
[rfc-2091]: https://github.com/rust-lang/rfcs/pull/2091 | ||
[caller]: https://doc.rust-lang.org/stable/std/panic/struct.Location.html#method.caller | ||
|
||
### `const fn` improvements | ||
|
||
There are [several core language features] you can now use in a `const fn`: | ||
|
||
* `if`, `if let`, and `match` | ||
* `while`, `while let`, and `loop` | ||
* the `&&` and `||` operators | ||
|
||
You can also [cast to a slice][cast-to-slice]: | ||
|
||
```rust | ||
const fn foo() { | ||
let x = [1, 2, 3, 4, 5]; | ||
|
||
// cast the array to a slice | ||
let y: &[_] = &x; | ||
} | ||
``` | ||
|
||
[several core language features]: https://github.com/rust-lang/rust/pull/72437/ | ||
[cast-to-slice]: https://github.com/rust-lang/rust/pull/73862/ | ||
|
||
### Library changes | ||
|
||
Keeping with the theme of `const fn` improvements, [`std::mem::forget` is now | ||
|
@RalfJung I found the source of why I thought
const fn
can have a lot of compile-time computation power, not just for use inconst
variables.