📜 ⬆️ ⬇️

Rust 1.20 Release

The Rust team is pleased to present Rust 1.20. Rust is a system programming language
Aiming at speed, security and parallel code execution.


If you have a previous version of Rust installed, just update it with:


$ rustup update stable 

If Rust is not installed yet, you can rustup with the appropriate
pages of our website and read the detailed release notes for Rust 1.20 on GitHub.


What is included in the stable version 1.20


In previous versions of Rust, you could define types, structures, and enumerations that have "associated functions":


 struct Struct; impl Struct { fn foo() { println!("foo -     Struct"); } } fn main() { Struct::foo(); } 

We call them "associated functions" because these functions are associated directly with the type itself, and not with any particular instance.


In Rust 1.20, the ability to define "associated constants" was added:


 struct Struct; impl Struct { const ID: u32 = 0; } fn main() { println!("the ID of Struct is: {}", Struct::ID); } 

Thus, the constant ID associated with Struct .
Like functions, associated constants work with both types and enumerations.


Types have an additional feature, useful in many situations. You can use associated constants as well as associated types: declaring them, but not assigning any value. The specific value must be assigned during the implementation of the type of any type.


 trait Trait { const ID: u32; } struct Struct; impl Trait for Struct { const ID: u32 = 5; } fn main() { println!("{}", Struct::ID); } 

Prior to this release, if you wanted to write a type representing floating point numbers, you had to do this:


 trait Float { fn nan() -> Self; fn infinity() -> Self; ... } 

It looks a bit cumbersome, but, more importantly, we could not use these functions in constant expressions, even if the functions returned the same value.
Because of this, Float must also provide constants:


 mod f32 { const NAN: f32 = 0.0f32 / 0.0f32; const INFINITY: f32 = 1.0f32 / 0.0f32; impl Float for f32 { fn nan() -> Self { f32::NAN } fn infinity() -> Self { f32::INFINITY } } } 

Associated constants allow you to do the same, but much easier.
Ad type:


 trait Float { const NAN: Self; const INFINITY: Self; ... } 

Implementation:


 mod f32 { impl Float for f32 { const NAN: f32 = 0.0f32 / 0.0f32; const INFINITY: f32 = 1.0f32 / 0.0f32; } } 

much clearer and more flexible.


Associated constants were proposed in RFC 195 , almost three years ago. A decent time! That RFC contained all sorts of associated elements, not just constants. Some of them we were able to implement faster than others. We work a lot on improving support for working with constant expressions in order to increase Rust’s meta-programming capabilities during compilation. In the future, additional opportunities will appear in this area.


In addition, we fixed the error when working with the include! macro include! in documentation tests: file paths were determined relative to the working directory, not the directory in which the current file is located.


See the detailed release notes for details.


Standard Library Stabilization


There are no super outstanding changes in this release, just a few useful improvements and continued work on stabilizing the standard library.


Macro unimplemented! now accepts a parameter to let the user know for what reason something was not implemented.


We have updated Unicode support to version 10.0.0 .


The functions min and max for floating-point types were rewritten to Rust , and are no longer dependent on cmath .


Introduced protection against Stack Clash vulnerability. The main changes are: stack probes and disabling additional manual checks for the mainstream stack on Linux . To enable protection, it is enough to compile the project in Rust 1.20, no changes in the code are required.


Three new sorting functions have been added to the standard library: slice::sort_unstable_by_key , slice::sort_unstable_by and slice::sort_unstable . As you noticed, all three contain "unstable" in the names. Stability is a property of the sorting algorithm, which is not always required, but previously there were no unstable sorting algorithms in the standard library. Both options are now available! To demonstrate the difference between these types of sorting, consider the list:


 rust crate package cargo 

The two words above, cargo and crate , begin with the letter c . A list sorted by the stable sorting algorithm only by the first letter should look like this:


 crate cargo package rust 

That is, if in the initial list the word crate preceded the word cargo , then in the sorted list it should be first. The unstable sorting algorithm can also produce the following result, but a variant with a modified sequence is also allowed:


 cargo crate package rust 

As you understand, fewer restrictions often allow you to create a faster algorithm. If sorting stability is not important to you, unstable sorting may be faster than a stable option. As usual, it is better to try both options and compare their speed. These sorting functions were added in RFC 1884 . Under the link you can find out more details, including the results of benchmarks.


The following APIs have also been stabilized:



See the detailed release notes for details.


Cargo Improvements


This release introduced some useful improvements to the cargo package manager. First and foremost, the authentication token for crates.io was stored in ~/.cargo/config . Typically, the access mask for configuration files is 644 , which means that all users are allowed to read. However, a secret token was stored in this file. We moved it into a separate ~/.cargo/credentials file, for which the access mask 600 can be set and it will be hidden from other users of the system.


If you used Cargo packages that create additional executable files, you know that their source code is stored in src/bin . But sometimes you may need to create some additional executable files that require a lot of code. In this case, the code can be stored in the src/bin/client.rs and src/bin/server.rs , and all the submodules of these files fall into one directory, which is inconvenient and creates confusion. Now we use the convention whereby files like src/bin/server/main.rs and src/bin/client/main.rs also used to create additional executable files. This makes it easier to distinguish between code.


See the detailed release notes for details.


Developers 1.20.0


A lot of people participated in the development of Rust 1.20. We could not achieve this.
without the participation of each of you. Thank!


Authors translation: red75prime and vitvakatu


')

Source: https://habr.com/ru/post/337058/


All Articles