@@ -25,23 +25,54 @@ appropriate page on our website, and check out the [detailed release notes for
25
25
26
26
## What's in 1.46.0 stable
27
27
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
+
32
63
33
64
### ` #[track_caller] `
34
65
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
36
67
this was implemented was not yet stable. Rust 1.46 stabilizes this feature.
37
68
38
69
[ better-errors ] : https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#useful-line-numbers-in-option-and-result-panic-messages
39
70
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:
45
76
46
77
``` rust
47
78
pub fn unwrap (self ) -> T {
@@ -72,28 +103,6 @@ on `std::panic::Location` to get access to this information.
72
103
[ rfc-2091 ] : https://github.com/rust-lang/rfcs/pull/2091
73
104
[ caller ] : https://doc.rust-lang.org/stable/std/panic/struct.Location.html#method.caller
74
105
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
-
97
106
### Library changes
98
107
99
108
Keeping with the theme of ` const fn ` improvements, [ ` std::mem::forget ` is now
0 commit comments