📜 ⬆️ ⬇️

Rust 1.21 release

The Rust team is pleased to present the release of Rust 1.21.0. Rust is a system programming language aimed at speed, security, and parallel code execution.


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


$ rustup update stable 

If you have not yet installed rustup , you can install it from the corresponding page of our website. Detailed notes for the release of Rust 1.21.0 can be found on GitHub.


What is included in the stable version 1.21.0


This release contains several small but useful language changes and new documentation.


The first change concerns literals. Consider the code:


 let x = &5; 

In Rust, it is similar to the following:


 let _x = 5; let x = &_x; 

That is, 5 will be pushed onto the stack or possibly into registers, and x will be a reference to it.


However, given that this is an integer literal, there is no reason to make the value so local. Imagine that we have a function that takes a 'static argument like std::thread::spawn . Then you could use x like this:


 use std::thread; fn main() { let x = &5; thread::spawn(move || { println!("{}", x); }); } 

This code will not build in past versions of Rust:


 error[E0597]: borrowed value does not live long enough --> src/main.rs:4:14 | 4 | let x = &5; | ^ does not live long enough ... 10 | } | - temporary value only lives until here | = note: borrowed value must be valid for the static lifetime... 

Due to the locality of 5 , the link to it also lives too little to meet the requirements of spawn .


But if you collect it from Rust 1.21, it will work. Why? If something to which the link was created can be put in static , we could "desugare" let x = &5; into something like:


 static FIVE: i32 = 5; let x = &FIVE; 

And once FIVE is static , then x is &'static i32 . So now Rust will work in such cases. For details, see RFC 1414 , which was adopted in January 2017, but started back in December 2015!


Now we run LLVM in parallel with code generation , which should reduce peak memory consumption.


RLS can now be installed via rustup by calling the rustup component add rls-preview . Many useful tools, such as RLS, Clippy and rustfmt , still require nightly Rust, but this is the first step to working on a stable channel. Expect further improvements in the future, but for now take a look at the preliminary version.


Now about the documentation improvements. First, if you look at the std::os that contains the functionality for working with operating systems, you will see not only Linux , the platform on which the documentation was compiled. We were upset for a long time that the official documentation was only for Linux. This is the first step to correct the situation.
although this is currently available only for the standard library , not for any package (crate). We hope to fix this in the future.


Next, the Cargo documentation is moving! Historically, the Cargo documentation was posted on doc.crates.io, which did not follow the release train model, although Cargo itself followed. This led to situations where some kind of functionality soon had to “merge” into the night Cargo, the documentation was updated, and over the next 12 weeks, users thought that everything was working, although it was not yet true. https://doc.rust-lang.org/cargo will be the new home for the Cargo documentation, although now this address is simply redirected to doc.crates.io. Future issues will relocate this Cargo documentation, and then doc.crates.io will be redirected to doc.rust-lang.org/cargo. Cargo documentation has long needed to be updated, so expect more news about it in the near future!


Finally, before this release, rustdoc had no documentation. Now this is fixed : a new book " rustdoc Book" has been added, available at https://doc.rust-lang.org/rustdoc . Now this documentation is very primitive, but over time it will improve.


See the release notes for details.


Stabilization in the standard library


There are not so many stabilizations in this release, but there is something that makes life very simple: due to the lack of generalization about integers (type-level integers), arrays only support types up to size 32. Now Clone . By the way, this caused a lot of ICE (internal compiler errors) when the type implemented only Copy , but not Clone .
For other types , the RFC has recently adopted a generalization about integers , which should remedy the situation. This change has not yet been implemented, but preparatory work is already underway.


Then Iterator::for_each was stabilized, making it possible to absorb the iterator for the sake of side effects without a for loop:


 //   for i in 0..10 { println!("{}", i); } //   (0..10).for_each(|i| println!("{}", i)); 

Which way better depends on the situation. In the above code, the for loop is simple, but if you are building a chain of iterators, the version with for_each can be clearer:


 //   for i in (0..100).map(|x| x + 1).filter(|x| x % 2 == 0) { println!("{}", i); } //   (0..100) .map(|x| x + 1) .filter(|x| x % 2 == 0) .for_each(|i| println!("{}", i)); 

Rc<T> and Arc<T> now implement From<&[T]> where T: Clone , From<str> , From<String> , From<Box<T>> where T: ?Sized , and From<Vec<T>> .


The max and min max Ord type are stabilized.


The (intrinsic) needs_drop .


Finally, std::mem::discriminant , allowing you to find out the active listing option without using the match operator.


See the release notes for details.


Functional Cargo


In addition to the above-mentioned changes to the Cargo documentation in this release, there is a big update: [patch] . Developed in RFC 1969 , the [patch] section of your Cargo.toml can be used when you want to replace parts of your dependency graph. This could have been done before, by [relace] . In short, [patch] is a new and more convenient [replace] . And although we have no plans to remove or declare obsolete [replace] , you should most likely use exactly [patch] .


How does [patch] ? Suppose we have the following Cargo.toml :


 [dependencies] foo = "1.2.3" 

Also, our package (crate) foo depends on the bar package, and we found an error in bar . To check this, we download the source bar code and update our Cargo.toml :


 [dependencies] foo = "1.2.3" [patch.crates-io] bar = { path = '/path/to/bar' } 

Now, when we perform cargo build , our local version of bar will be used, not the version from crates.io , on which foo actually depends.


See the documentation for details.


Also:



See the release notes for details.


Developers 1.21.0


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


From the translator: I thank sasha_gav and vitvakatu for help in translating


')

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


All Articles