|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Announcing Rust 1.42.0" |
| 4 | +author: The Rust Release Team |
| 5 | +release: true |
| 6 | +--- |
| 7 | + |
| 8 | +# Announcing Rust 1.42.0 |
| 9 | + |
| 10 | +The Rust team is happy to announce a new version of Rust, 1.42.0. Rust is a programming language that is empowering everyone to build reliable and efficient software. |
| 11 | + |
| 12 | +If you have a previous version of Rust installed via rustup, getting Rust 1.42.0 is as easy as: |
| 13 | + |
| 14 | +```console |
| 15 | +rustup update stable |
| 16 | +``` |
| 17 | + |
| 18 | +If you don't have it already, you can [get `rustup`][install] from the appropriate page on our website, and check out the [detailed release notes for 1.42.0][notes] on GitHub. |
| 19 | + |
| 20 | +[install]: https://www.rust-lang.org/install.html |
| 21 | +[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1420-2020-03-12 |
| 22 | + |
| 23 | +## What's in 1.42.0 stable |
| 24 | + |
| 25 | +The highlights of Rust 1.42.0 include: more useful panic messages when `unwrap`ping, subslice patterns, the deprecation of `Error::description`, and more. See the [detailed release notes][notes] to learn about other changes not covered by this post. |
| 26 | + |
| 27 | +### Useful line numbers in `Option` and `Result` panic messages |
| 28 | + |
| 29 | +In Rust 1.41.1, calling `unwrap()` on an `Option::None` value would produce an error message looking something like this: |
| 30 | + |
| 31 | +``` |
| 32 | +thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /.../src/libcore/macros/mod.rs:15:40 |
| 33 | +``` |
| 34 | + |
| 35 | +Similarly, the line numbers in the panic messages generated by `unwrap_err`, `expect`, and `expect_err`, and the corresponding methods on the `Result` type, also refer to `core` internals. |
| 36 | + |
| 37 | +In Rust 1.42.0, all eight of these functions produce panic messages that provide the line number where they were invoked. The new error messages look something like this: |
| 38 | + |
| 39 | +``` |
| 40 | +thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:2:5 |
| 41 | +``` |
| 42 | + |
| 43 | +This means that the invalid call to `unwrap` was on line 2 of `src/main.rs`. |
| 44 | + |
| 45 | +This behavior is made possible by an annotation, `#[track_caller]`. This annotation is not yet available to use in stable Rust; if you are interested in using it in your own code, you can follow its progress by watching [this tracking issue][track-caller-tracking-issue]. |
| 46 | + |
| 47 | +[track-caller-tracking-issue]: https://github.com/rust-lang/rust/issues/47809 |
| 48 | + |
| 49 | +### Subslice patterns |
| 50 | + |
| 51 | +[slice patterns]: https://blog.rust-lang.org/2018/05/10/Rust-1.26.html#basic-slice-patterns |
| 52 | + |
| 53 | +In Rust 1.26, we stabilized "[slice patterns]," which let you `match` on slices. They looked like this: |
| 54 | + |
| 55 | +```rust |
| 56 | +fn foo(words: &[&str]) { |
| 57 | + match words { |
| 58 | + [] => println!("empty slice!"), |
| 59 | + [one] => println!("one element: {:?}", one), |
| 60 | + [one, two] => println!("two elements: {:?} {:?}", one, two), |
| 61 | + _ => println!("I'm not sure how many elements!"), |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +This allowed you to match on slices, but was fairly limited. You had to choose the exact sizes |
| 67 | +you wished to support, and had to have a catch-all arm for size you didn't want to support. |
| 68 | + |
| 69 | +In Rust 1.42, we have [expanded support for matching on parts of a slice][67712]: |
| 70 | + |
| 71 | +```rust |
| 72 | +fn foo(words: &[&str]) { |
| 73 | + match words { |
| 74 | + ["Hello", "World", "!", ..] => println!("Hello World!"), |
| 75 | + ["Foo", "Bar", ..] => println!("Baz"), |
| 76 | + rest => println!("{:?}", rest), |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +The `..` is called a "rest pattern," because it matches the rest of the slice. The above example uses the rest pattern at the end of a slice, but you can also use it in other ways: |
| 82 | + |
| 83 | +```rust |
| 84 | +fn foo(words: &[&str]) { |
| 85 | + match words { |
| 86 | + // Ignore everything but the last element, which must be "!". |
| 87 | + [.., "!"] => println!("!!!"), |
| 88 | + |
| 89 | + // `start` is a slice of everything except the last element, which must be "z". |
| 90 | + [start @ .., "z"] => println!("starts with: {:?}", start), |
| 91 | + |
| 92 | + // `end` is a slice of everything but the first element, which must be "a". |
| 93 | + ["a", end @ ..] => println!("ends with: {:?}", end), |
| 94 | + |
| 95 | + rest => println!("{:?}", rest), |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +If you're interested in learning more, we published [a post on the Inside Rust blog](https://blog.rust-lang.org/inside-rust/2020/03/04/recent-future-pattern-matching-improvements.html) discussing these changes as well as more improvements to pattern matching that we may bring to stable in the future! You can also read more about slice patterns in [Thomas Hartmann's post](https://thomashartmann.dev/blog/feature(slice_patterns)/). |
| 101 | + |
| 102 | + |
| 103 | +### [`matches!`] |
| 104 | + |
| 105 | +This release of Rust stabilizes a new macro, [`matches!`](https://doc.rust-lang.org/nightly/std/macro.matches.html). This macro accepts an expression and a pattern, and returns true if the pattern matches the expression. In other words: |
| 106 | + |
| 107 | +```rust |
| 108 | +// Using a match expression: |
| 109 | +match self.partial_cmp(other) { |
| 110 | + Some(Less) => true, |
| 111 | + _ => false, |
| 112 | +} |
| 113 | + |
| 114 | +// Using the `matches!` macro: |
| 115 | +matches!(self.partial_cmp(other), Some(Less)) |
| 116 | +``` |
| 117 | + |
| 118 | +You can also use features like `|` patterns and `if` guards: |
| 119 | + |
| 120 | +```rust |
| 121 | +let foo = 'f'; |
| 122 | +assert!(matches!(foo, 'A'..='Z' | 'a'..='z')); |
| 123 | + |
| 124 | +let bar = Some(4); |
| 125 | +assert!(matches!(bar, Some(x) if x > 2)); |
| 126 | +``` |
| 127 | + |
| 128 | +### `use proc_macro::TokenStream;` now works |
| 129 | + |
| 130 | +In Rust 2018, we [removed the need for `extern crate`](https://doc.rust-lang.org/stable/edition-guide/rust-2018/module-system/path-clarity.html#no-more-extern-crate). But procedural macros were a bit special, and so when you were writing a procedural macro, you still needed to say `extern crate proc_macro;`. |
| 131 | + |
| 132 | +In this release, if you are using Cargo, [you no longer need this line when working with the 2018 edition; you can use `use` like any other crate][cargo/7700]. Given that most projects will already have a line similar to `use proc_macro::TokenStream;`, this change will mean that you can delete the `extern crate proc_macro;` line and your code will still work. This change is small, but brings procedural macros closer to regular code. |
| 133 | + |
| 134 | +### Libraries |
| 135 | + |
| 136 | +- [`iter::Empty<T>` now implements `Send` and `Sync` for any `T`.][68348] |
| 137 | +- [`Pin::{map_unchecked, map_unchecked_mut}` no longer require the return type |
| 138 | + to implement `Sized`.][67935] |
| 139 | +- [`io::Cursor` now implements `PartialEq` and `Eq`.][67233] |
| 140 | +- [`Layout::new` is now `const`.][66254] |
| 141 | + |
| 142 | +### Stabilized APIs |
| 143 | + |
| 144 | +- [`CondVar::wait_while`] & [`CondVar::wait_timeout_while`] |
| 145 | +- [`DebugMap::key`] & [`DebugMap::value`] |
| 146 | +- [`ManuallyDrop::take`] |
| 147 | +- [`ptr::slice_from_raw_parts_mut`] & [`ptr::slice_from_raw_parts`] |
| 148 | + |
| 149 | +[`DebugMap::key`]: https://doc.rust-lang.org/stable/std/fmt/struct.DebugMap.html#method.key |
| 150 | +[`DebugMap::value`]: https://doc.rust-lang.org/stable/std/fmt/struct.DebugMap.html#method.value |
| 151 | +[`ManuallyDrop::take`]: https://doc.rust-lang.org/stable/std/mem/struct.ManuallyDrop.html#method.take |
| 152 | +[`matches!`]: https://doc.rust-lang.org/stable/std/macro.matches.html |
| 153 | +[`ptr::slice_from_raw_parts_mut`]: https://doc.rust-lang.org/stable/std/ptr/fn.slice_from_raw_parts_mut.html |
| 154 | +[`ptr::slice_from_raw_parts`]: https://doc.rust-lang.org/stable/std/ptr/fn.slice_from_raw_parts.html |
| 155 | +[`CondVar::wait_while`]: https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html#method.wait_while |
| 156 | +[`CondVar::wait_timeout_while`]: https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html#method.wait_timeout_while |
| 157 | +[68253]: https://github.com/rust-lang/rust/pull/68253/ |
| 158 | +[68348]: https://github.com/rust-lang/rust/pull/68348/ |
| 159 | +[67935]: https://github.com/rust-lang/rust/pull/67935/ |
| 160 | +[68339]: https://github.com/rust-lang/rust/pull/68339/ |
| 161 | +[68122]: https://github.com/rust-lang/rust/pull/68122/ |
| 162 | +[67712]: https://github.com/rust-lang/rust/pull/67712/ |
| 163 | +[67887]: https://github.com/rust-lang/rust/pull/67887/ |
| 164 | +[67131]: https://github.com/rust-lang/rust/pull/67131/ |
| 165 | +[67233]: https://github.com/rust-lang/rust/pull/67233/ |
| 166 | +[66899]: https://github.com/rust-lang/rust/pull/66899/ |
| 167 | +[66919]: https://github.com/rust-lang/rust/pull/66919/ |
| 168 | +[66254]: https://github.com/rust-lang/rust/pull/66254/ |
| 169 | +[cargo/7700]: https://github.com/rust-lang/cargo/pull/7700 |
| 170 | + |
| 171 | +### Other changes |
| 172 | + |
| 173 | +[relnotes-cargo]: https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-142-2020-03-12 |
| 174 | + |
| 175 | +There are other changes in the Rust 1.42.0 release: check out what changed in [Rust][notes] and [Cargo][relnotes-cargo]. |
| 176 | + |
| 177 | + |
| 178 | +### Compatibility Notes |
| 179 | + |
| 180 | +We have two notable compatibility notes this release: a deprecation in the standard library, and a demotion of 32-bit Apple targets to Tier 3. |
| 181 | + |
| 182 | +#### Error::Description is deprecated |
| 183 | + |
| 184 | +Sometimes, mistakes are made. The `Error::description` method is now considered to be one of those mistakes. The problem is with its type signature: |
| 185 | + |
| 186 | +```rust |
| 187 | +fn description(&self) -> &str |
| 188 | +``` |
| 189 | + |
| 190 | +Because `description` returns a `&str`, it is not nearly as useful as we wished it would be. This means that you basically need to return the contents of an `Error` verbatim; if you wanted to say, use formatting to produce a nicer description, that is impossible: you'd need to return a `String`. Instead, error types should implement the `Display`/`Debug` traits to provide the description of the error. |
| 191 | + |
| 192 | +This API has existed since Rust 1.0. We've been working towards this goal for a long time: back in Rust 1.27, we ["soft deprecated" this method](https://github.com/rust-lang/rust/pull/50163). What that meant in practice was, we gave the function a default implementation. This means that users were no longer forced to implement this method when implementing the `Error` trait. In this release, [we mark it as *actually* deprecated][66919], and took some steps to de-emphasize the method in `Error`'s documentation. Due to our stability policy, `description` will never be removed, and so this is as far as we can go. |
| 193 | + |
| 194 | +#### Downgrading 32-bit Apple targets |
| 195 | + |
| 196 | +Apple is no longer supporting 32-bit targets, and so, neither are we. They have been downgraded to Tier 3 support by the project. For more details on this, check out [this post](https://blog.rust-lang.org/2020/01/03/reducing-support-for-32-bit-apple-targets.html) from back in January, which covers everything in detail. |
| 197 | + |
| 198 | +## Contributors to 1.42.0 |
| 199 | + |
| 200 | +Many people came together to create Rust 1.42.0. We couldn't have done it without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.42.0/) |
0 commit comments