Rust 1.39.0 release: async / await, attributes for function parameters, new constant functions

The Rust team is pleased to announce the release of a new version, 1.39.0. Rust is a programming language that allows everyone to create reliable and efficient software.







If you installed the previous version of Rust using rustup



, then to upgrade to version 1.39.0 you just need to run the following command:







 $ rustup update stable
      
      





If you have not already installed rustup



, you can install it from the corresponding page of our website, as well as see detailed release notes on GitHub.







What is included in the stable version 1.39.0



The most significant innovations include the async



/ .await



, shared references to movable values ​​in match-guards



and the attributes of function parameters. See detailed release notes for more information.







With .await



over, meet async fn





Earlier in Rust 1.36.0, we announced the availability of the Future



trait. Then we noted that:







We hope that this innovation will allow popular crates, libraries, and the entire ecosystem to prepare for the async



/ .await



, which is planned to be stabilized for the near future.

The promise is given - the promise is fulfilled. We are pleased to announce that the long-awaited async



/ .await



, which allows us to define asynchronous functions and blocks ( async



) and wait for their execution ( .await



), is finally stable!







An asynchronous function defined using the async fn



syntax (instead of the usual fn



) does nothing except that when called, it returns an object that implements the Future



trait. This object is a suspended calculation, which can be completed with the .await



syntax. In addition, async fn



, async { ... }



and async move { ... }



act as closures and can be used to define asynchronous literals.







You can read more about the release of async



/ .await



in the Niko Matsakis blog post .







Shared links to roaming values ​​in match-guards





When matching an image, a variable known as binding can be bound in one of the following ways:









Now it is allowed to use template variables in the if-constraints of the template if ownership is transferred to this variable (i.e. the variable in the binds-by-move template). Previously, the following code would be discarded:







 fn main() { let array: Box<[u8; 4]> = Box::new([1, 2, 3, 4]); match array { nums // ---- `nums`    (by move). if nums.iter().sum::<u8>() == 10 // ^------ `.iter()`     `nums` => { drop(nums); // ----------- `nums`      } _ => unreachable!(), } }
      
      





Starting with Rust 1.39.0 , the compiler will accept the fragment above without warnings and errors. We hope that this innovation will increase the usability of the match



expression.







Attributes for function parameters



Starting with Rust 1.39.0, attributes can be applied to function parameters, closures, and function pointers. Previously, the len()



function could be represented as follows:







 #[cfg(windows)] fn len(slice: &[u16]) -> usize { slice.len() } #[cfg(not(windows))] fn len(slice: &[u8]) -> usize { slice.len() }
      
      





But Rust 1.39.0 makes writing the code above much simpler:







 fn len( #[cfg(windows)] slice: &[u16], //        Windows. #[cfg(not(windows))] slice: &[u8], //       . ) -> usize { slice.len() }
      
      





Usable attributes include:







  1. Conditional compilation: cfg



    and cfg_attr



    ;







  2. Manage checks: allow



    , warn



    , deny



    and forbid



    ;







  3. Helper attributes used by procedural attribute macros for syntax elements.







    We hope this innovation will help the ecosystem write more expressive macro-based DSLs.









Borrowing check migration warnings become serious bugs in Rust 2018



In release 1.35.0, we announced that the new borrowing analyzer (NLL) came to the editors of Rust 2015 after it was released to the editors of Rust 2018 in version 1.31 .







As stated in release 1.35.0, the previous borrowing analyzer was designed with bugs that allowed insecure memory usage. These errors were corrected using a new analyzer that implements NLL. Since these fixes could break working code, the Rust team decided to introduce them gradually, checking that the previous analyzer should accept the code, which the new analyzer would reject. If so, then the errors will instead become warnings instead.







Starting with Rust 1.39.0, these warnings are now bugs in the Rust 2018 edition .

In the next release of Rust 1.40.0, this rule will also be applied to the Rust 2015 revision code , which will completely eliminate the old borrowing analyzer from the compiler.







If you are affected by these changes, or want to know more, check out the Niko Matsakis blog post .







More constant functions in the standard library



Starting with Rust 1.39.0, the following functions are marked as const fn



( const fn



):









Stable functions in the standard library



The following functions were stabilized in Rust 1.39.0:









Other changes



The syntax , the cargo package manager, and the Clippy analyzer have also undergone some changes.







Please read the compatibility notes to see if these changes affect you.







Members 1.39.0



A lot of people came together to create Rust 1.39.0. We could not have done this without all of you, thank you !







From translators



With any questions on the Rust language, you can be helped in the Russian-language Telegram chat or in a similar chat for newcomers .







This article was jointly translated by andreevlex , blandger , funkill and Hippolot .








All Articles