I present to your attention the translation of the publication of the new version of the beloved programming language Rust .
The Rust programming language development team is pleased to announce a new version, 1.35.0. Rust is a programming language that allows everyone to develop reliable and fast software.
If you installed the previous version of Rust using rustup
tools, getting the current version will not be difficult:
$ rustup update stable
If you still have no rustup
, you can get it from the corresponding page on our website. A detailed review of this release is available on GitHub.
The main innovations of this release are the implementation of FnOnce, FnMut
and Fn
FnMut
on the Box<dyn FnOnce>
, Box<dyn FnMut>
and Box<dyn Fn>
structures, respectively.
Also, embedded functions (closures) can be converted to unsafe function pointers. Macro dbg!
, entered in Rust 1.32.0 , can now be called without specifying arguments.
Moreover, this release has made many stabilizations of the standard library. Below are the most significant, but available and detailed analysis of these.
Fn*
traits are implemented on Box<dyn Fn*>
In Rust 1.35.0, FnOnce, FnMut
and Fn
FnMut
implemented on Box<dyn FnOnce>
, Box<dyn FnMut>
and Box<dyn Fn>
respectively.
In the past, if you wanted to call a function that was encapsulated in Box
, you had to use FnBox
, since Box<dyn FnOnce>
objects and the like did not implement the corresponding Fn*
traits. It also prevented the transfer of Box
encapsulated functions to code waiting for the Fn
trait implementer (it was proposed to create temporary inline functions).
This was caused by the inability of the compiler to detect such implementations. This flaw was eliminated with the introduction of unsized_locals
.
However, now you can use Box
encapsulated functions even in those places that expect the implementation of a functional trait. For example, the code below compiles without errors:
fn foo(x: Box<dyn Fn(u8) -> u8>) -> Vec<u8> { vec![1, 2, 3, 4].into_iter().map(x).collect() }
Box<dyn FnOnce>
can be invoked without Box<dyn FnOnce>
fuss:
fn foo(x: Box<dyn FnOnce()>) { x() }
Since Rust 1.19.0, it has become possible to convert embedded functions that did not capture the environment into function pointers. For example, you could write:
fn twice(x: u8, f: fn(u8) -> u8) -> u8 { f(f(x)) } fn main() { assert_eq!(42, twice(0, |x| x + 21)); }
But unfortunately, this feature has not been extended to unsafe function pointers. This release introduced the changes described above:
/// , `unsafe fn`. unsafe fn call_unsafe_fn_ptr(f: unsafe fn()) { f() } fn main() { // : // // unsafe { call_unsafe_fn_ptr(|| { dbg!(); }); } }
dbg!()
With no argumentsDue to the abundance of println!
calls println!
as kolkhoz debuggers, the dbg!
macro was introduced in Rust 1.32.0 dbg!
. Recall that this macro allows you to quickly capture the result of a certain expression with context:
fn main() { let mut x = 0; if dbg!(x == 1) { x += 1; } dbg!(x); }
The above lines of code will type in the terminal the result of the expression x == 1
and x
respectively:
[src/main.rs:4] x == 1 = false [src/main.rs:8] x = 0
As mentioned in the previous section, where a higher-order function can be called call_unsafe_fn_ptr
, dbg!()
also to be called without specifying arguments. This can be extremely useful for detecting selected software branches:
fn main() { let condition = true; if condition { dbg!(); // [src/main.rs:5] } }
In Rust 1.35.0, many of the components of the standard library have been stabilized. In addition to this, some implementations have been made that you can read about here .
With this release, new copysign
methods copysign
been added to floating point primitives (more specifically, f32
and f64
):
As you might guess from the name of the method, you can use them to copy the sign of one number to another:
fn main() { assert_eq!(3.5_f32.copysign(-0.42), -3.5); }
Range
contains a specific valueRust 1.35.0 acquired a couple of new methods on Range*
structures:
Range::contains
RangeFrom::contains
RangeTo::contains
RangeInclusive::contains
RangeToInclusive::contains
With these methods, you can easily check whether a particular value is in a range. For example, you can write:
fn main() { if (0..=10).contains(&5) { println!("5 [0; 10]."); } }
RefCell
With the arrival of Rust 1.35.0, you can translate and split the borrowed RefCell
value into a set of borrowed values into different components of the borrowed data:
RefCell
value via an inline functionThis release represents the convenient replace_with
method declared on the RefCell
structure:
This release introduces the function ptr::hash
, which accepts a raw pointer for hashing. Using ptr::hash
can prevent hashing of the specified or referenced value instead of the address itself.
Option<&T>
With the start of Rust 1.0.0, the Option::cloned
methods on Option<&T>
and Option<&mut T>
allowed you to clone content if present ( Some(_)
). However, cloning can sometimes be an expensive operation, and the opt.cloned()
methods opt.cloned()
not describe any prompts.
This release introduced:
Option::copied
for Option<&T>
and Option<&mut T>
The opt.copied()
functionality is the same as opt.cloned()
. However, the method described above asks for T: Copy
conditions, the failure of which will cause a compilation error.
Clippy, a tool that detects common imperfections to improve the quality of the code, has acquired drop_bounds
. It works in cases where the generic function requests that the T: Drop
condition be satisfied:
fn foo<T: Drop>(x: T) {}
This is often a mistake, since primitives do not implement Drop
. Moreover, T: Drop
does not cover types like String
that do not have visual destructor behavior, but rather the result of embedded types (like Vec<u8>
).
In addition to drop_bounds
, this release splits redundant_closure
into redundant_closure
and redundant_closure_for_method_calls
.
Read the detailed release of Clippy here .
A detailed description of the changes to Cargo is available here .
Many people came together to create Rust 1.35.0. We could not do this without all of you, thank you !
With any questions about the Rust language, they will be able to help you in the Russian-language Telegram chat , or in a similar chat for newcomers .
Source: https://habr.com/ru/post/453338/
All Articles