We are pleased to introduce the new version of Rust 1.9. Rust is a system programming language aimed at safe work with memory, speed and parallel code execution.
As usual, you can install Rust 1.9 from the corresponding page of the official site, and also get acquainted with the detailed list of changes in this version on GitHub. This release included about 1400 patches.
The biggest change in Rust 1.9 is the stabilization of the std::panic
module, which provides methods for stopping the process of unwinding a stack triggered by panic:
use std::panic; let result = panic::catch_unwind(|| { println!("!"); }); assert!(result.is_ok()); let result = panic::catch_unwind(|| { panic!(" !"); }); assert!(result.is_err());
This interface was defined in RFC 1236 .
In general, Rust distinguishes between two types of error situations:
Expected problems usually occur due to circumstances that the program cannot control; reliable code must be ready for any trouble arising in its environment. Expected problems are handled in Rust using the Result
type , which allows the function to return the problem information to the caller, and the caller can already handle the error. This is a fairly accurate way to handle errors.
Unexpected problems are bugs : they occur because of a breach of contract or assertion. Since their occurrence is unexpected, there is not much point in accurately processing such errors. Instead, Rust uses the "fail fast" approach — such errors cause panic, which by default starts unwinding the stack of a thread that has encountered an error. In this case, only destructors are executed - no other code is executed. Other threads will continue to run, but they will find a panic when trying to exchange data with a panic stream (through channels or through shared memory). Thus, panic interrupts execution, down to some kind of "isolation boundary". The code on the other side of the border can continue to work, and if desired, restore the program from a state of panic, in a "rude" way. For example, the server does not necessarily fall due to a failed assert in one of its threads.
The new catch_unwind
interface provides a way to introduce additional isolation boundaries within the stream . There are a couple of examples where this is useful:
The first case was a potentially undefined behavior. In practice, unwinding to another language often leads to segfault. By allowing us to catch a panic, we simplify exporting a code to Rust as an API C - now we can catch a panic on the border of the transition to C and convert it into a return code.
The second case is motivated by thread pool libraries. If a thread in a pool is panicking, it’s usually not necessary to kill the thread itself. Instead, you need to catch a panic and report it to the client pool. The catch_unwind
interface has a resume_unwind
function resume_unwind
, which can be used to restart the panic process on the client side of the pool, to which it belongs.
In both cases, we introduce an additional isolation boundary within the stream, and then convert the panic into another kind of error.
catch_unwind
final note: why is catch_unwind
, not catch_panic
? Work is underway to add another strategy for panicking: interrupting the entire process (abort). In this case, it is possible that a common hook will be executed. For some applications, this is the most reasonable way to handle programming errors, and preventing stack unwinding can provide improved performance and smaller code size.
A new attribute has become available for authors of libraries: #[deprecated]
. This attribute allows you to mark an outdated interface, and library users will receive a warning when using it. In this case, you can specify a new recommended replacement interface. Obsolete interface warnings have long been used in the standard library, and now, thanks to RFC 1270 , they can be used throughout the entire Rust ecosystem.
Now compiled standard libraries for several new platforms are published:
mips-unknown-linux-musl
,mipsel-unknown-linux-musl
, andi586-pc-windows-msvc
.The first two platforms are particularly interesting in terms of cross-compiling; See the recent publication for rustup
for rustup
.
The time complexity of checking variables for equivalence during type unification is reduced from O (n!) To O (n). As a result, some code samples are compiled much faster.
In this release, specialization is first used in the standard library. Currently specialization is only available at nightly . It allows you to specialize generic code for more specific types.
One example where this happens in the standard library is the conversion from the string slice ( &str
) to the owned string ( String
). The to_string
method is taken from a generic interface, which was previously slower than the special to_owned
method. Now these functions are equivalent .
By implementing this simple case, we will proceed to other places where you can improve performance with the help of specialization.
In 1.9 about 80 library functions are stabilized. The most noticeable change is the std::panic
module described earlier. Besides him there are several other things.
Work with network
TcpStream
, TcpListener
and UdpSocket
have methods for configuring the connection.SocketAddr
and its variants have gotten convenient set_ip()
and set_port()
methods.Collections
BTreeSet
and HashSet
have got the methods take()
, replace()
, and get()
, which allow you to get back ownership of the original key.OsString
received several new methods and became more like a String
.copy_from_slice()
, a secure form of memcpy
.Encodings
char
can now be decoded to UTF-16.Pointers
as_ref()
and as_mut()
, which return Option<&T>
, converting null pointers to None
.ptr::{read,write}_volatile()
allows volatile reading and writing on a raw pointer.Finally, many types in libcore
did not have the Debug
type implementation. This is fixed in release 1.9.
Cargo has two big changes.
First, now several Cargo processes can work simultaneously .
Secondly, a new flag has been added - RUSTFLAGS
. This flag allows you to specify arbitrary flags that will be passed to rustc
through the environment. This is useful, for example, for packers.
127 people participated in the release of version 1.9. Thank you very much!
Source: https://habr.com/ru/post/301994/
All Articles