|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Announcing Rust 1.46.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.46.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.46.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] from the |
| 20 | +appropriate page on our website, and check out the [detailed release notes for |
| 21 | +1.46.0][notes] on GitHub. |
| 22 | + |
| 23 | +[install]: https://www.rust-lang.org/tools/install |
| 24 | +[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1460-2020-08-27 |
| 25 | + |
| 26 | +## What's in 1.46.0 stable |
| 27 | + |
| 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 | + |
| 63 | + |
| 64 | +### `#[track_caller]` |
| 65 | + |
| 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 |
| 67 | +this was implemented was not yet stable. Rust 1.46 stabilizes this feature. |
| 68 | + |
| 69 | +[better-errors]: https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#useful-line-numbers-in-option-and-result-panic-messages |
| 70 | + |
| 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: |
| 76 | + |
| 77 | +```rust |
| 78 | +pub fn unwrap(self) -> T { |
| 79 | + match self { |
| 80 | + Some(val) => val, |
| 81 | + None => panic!("called `Option::unwrap()` on a `None` value"), |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +It now looks like this: |
| 87 | + |
| 88 | +```rust |
| 89 | +#[track_caller] |
| 90 | +pub fn unwrap(self) -> T { |
| 91 | + match self { |
| 92 | + Some(val) => val, |
| 93 | + None => panic!("called `Option::unwrap()` on a `None` value"), |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +That's it! |
| 99 | + |
| 100 | +If you are implementing a panic hook yourself, you can use the [caller] method |
| 101 | +on `std::panic::Location` to get access to this information. |
| 102 | + |
| 103 | +[rfc-2091]: https://github.com/rust-lang/rfcs/pull/2091 |
| 104 | +[caller]: https://doc.rust-lang.org/stable/std/panic/struct.Location.html#method.caller |
| 105 | + |
| 106 | +### Library changes |
| 107 | + |
| 108 | +Keeping with the theme of `const fn` improvements, [`std::mem::forget` is now |
| 109 | +a `const fn`][forget]. Additionally, two new APIs were stabilized this release: |
| 110 | + |
| 111 | +* [`Option::zip`][zip] |
| 112 | +* [`vec::Drain::as_slice`][as_slice] |
| 113 | + |
| 114 | +[forget]: https://github.com/rust-lang/rust/pull/73887/ |
| 115 | +[zip]: https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.zip |
| 116 | +[as_slice]: https://doc.rust-lang.org/stable/std/vec/struct.Drain.html#method.as_slice |
| 117 | + |
| 118 | +See the [detailed release notes][notes] for more. |
| 119 | + |
| 120 | +### Other changes |
| 121 | + |
| 122 | +[relnotes-cargo]: https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-146-2020-08-27 |
| 123 | +[relnotes-clippy]: https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-146 |
| 124 | + |
| 125 | +There are other changes in the Rust 1.46.0 release: check out what changed in |
| 126 | +[Rust][notes], [Cargo][relnotes-cargo], and [Clippy][relnotes-clippy]. |
| 127 | + |
| 128 | +## Contributors to 1.46.0 |
| 129 | + |
| 130 | +Many people came together to create Rust 1.46.0. We couldn't have done it |
| 131 | +without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.46.0/) |
0 commit comments