Skip to content

Commit

Permalink
various improvements to the 1.46 blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Aug 27, 2020
1 parent bfd17f5 commit 1b6d03e
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions posts/2020-08-27-Rust-1.46.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@pickfire

pickfire Aug 28, 2020

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!

@RalfJung I found the source of why I thought const fn can have a lot of compile-time computation power, not just for use in const variables.

This comment has been minimized.

Copy link
@RalfJung

RalfJung Aug 28, 2020

Member

"compile-time computation power" is about const items. This says nothing about optimizations.

This comment has been minimized.

Copy link
@pickfire

pickfire Aug 28, 2020

Well, you are right. But I do imply that compile-time computation power means more power, means more optimizations, means faster binary. Or maybe I thought think about it wrongly.

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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1b6d03e

Please sign in to comment.