|
| 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 | +In Rust 1.26, we stabilzed "slice patterns," which let you `match` on slices. They looked like this: |
| 52 | + |
| 53 | +```rust |
| 54 | +fn foo(words: &[&str]) { |
| 55 | + match words { |
| 56 | + [] => println!("empty slice!"), |
| 57 | + [one] => println!("one element: {:?}", one), |
| 58 | + [one, two] => println!("two elements: {:?} {:?}", one, two), |
| 59 | + _ => println!("I'm not sure how many elements!"), |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +This allowed you to match on slices, but was fairly limited. You had to choose the exact sizes |
| 65 | +you wished to support, and had to have a catch-all arm for size you didn't want to support. |
| 66 | + |
| 67 | +In Rust 1.42, we have [expanded support for matching on parts of a slice][67712]: |
| 68 | + |
| 69 | +```rust |
| 70 | +fn foo(words: &[&str]) { |
| 71 | + match words { |
| 72 | + ["Hello", "World", "!", ..] => println!("Hello World!"), |
| 73 | + ["Foo", "Bar", ..] => println!("Baz"), |
| 74 | + rest => println!("{:?}", rest), |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +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: |
| 80 | + |
| 81 | +```rust |
| 82 | +fn foo(words: &[&str]) { |
| 83 | + match words { |
| 84 | + // ignore everything but the last element, which must be "!" |
| 85 | + [.., "!"] => println!("!!!"), |
| 86 | + |
| 87 | + // start is a slice of everything except the last element, which must be "z" |
| 88 | + [start @ .., "z"] => println!("starts with: {:?}", start), |
| 89 | + |
| 90 | + // end is a slice of everything but the first element, which must be "a" |
| 91 | + ["a", end @ ..] => println!("ends with: {:?}", end), |
| 92 | + |
| 93 | + rest => println!("{:?}", rest), |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +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! |
| 99 | + |
| 100 | + |
| 101 | +### [`matches!`] |
| 102 | + |
| 103 | +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: |
| 104 | + |
| 105 | +```rust |
| 106 | +// with the match keyword |
| 107 | +match self.partial_cmp(other) { |
| 108 | + Some(Less) => true, |
| 109 | + _ => false, |
| 110 | +} |
| 111 | + |
| 112 | +// with the matches! macro |
| 113 | +matches!(self.partial_cmp(other), Some(Less)) |
| 114 | +``` |
| 115 | + |
| 116 | +You can also use features like `|` patterns and `if` clauses: |
| 117 | + |
| 118 | +```rust |
| 119 | +let foo = 'f'; |
| 120 | +assert!(matches!(foo, 'A'..='Z' | 'a'..='z')); |
| 121 | + |
| 122 | +let bar = Some(4); |
| 123 | +assert!(matches!(bar, Some(x) if x > 2)); |
| 124 | +``` |
| 125 | + |
| 126 | +### `use proc_macro::TokenStream;` now works |
| 127 | + |
| 128 | +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;`. |
| 129 | + |
| 130 | +In this release, [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. |
| 131 | + |
| 132 | +### Libraries |
| 133 | + |
| 134 | +- [`iter::Empty<T>` now implements `Send` and `Sync` for any `T`.][68348] |
| 135 | +- [`Pin::{map_unchecked, map_unchecked_mut}` no longer require the return type |
| 136 | + to implement `Sized`.][67935] |
| 137 | +- [`io::Cursor` now implements `PartialEq` and `Eq`.][67233] |
| 138 | +- [`Layout::new` is now `const`.][66254] |
| 139 | + |
| 140 | +### Stabilized APIs |
| 141 | + |
| 142 | +- [`CondVar::wait_while`] & [`CondVar::wait_timeout_while`] |
| 143 | +- [`DebugMap::key`] & [`DebugMap::value`] |
| 144 | +- [`ManuallyDrop::take`] |
| 145 | +- [`ptr::slice_from_raw_parts_mut`] & [`ptr::slice_from_raw_parts`] |
| 146 | + |
| 147 | +### Other changes |
| 148 | + |
| 149 | +[relnotes-cargo]: https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-142-2020-03-12 |
| 150 | +[relnotes-clippy]: TODO |
| 151 | + |
| 152 | +There are other changes in the Rust 1.42.0 release: check out what changed in [Rust][notes], [Cargo][relnotes-cargo], and [Clippy][relnotes-clippy]. |
| 153 | + |
| 154 | + |
| 155 | +### Compatibility Notes |
| 156 | + |
| 157 | +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. |
| 158 | + |
| 159 | +#### Error::Description is deprecated |
| 160 | + |
| 161 | +Sometimes, mistakes are made. The `Error::description` method is now considered to be one of those mistakes. The problem is with its type signature: |
| 162 | + |
| 163 | + fn description(&self) -> &str { |
| 164 | + |
| 165 | +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. |
| 166 | + |
| 167 | +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. |
| 168 | + |
| 169 | +#### Downgrading 32-bit Apple targets |
| 170 | + |
| 171 | +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. |
| 172 | + |
| 173 | +## Contributors to 1.42.0 |
| 174 | + |
| 175 | +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