scope streams, I

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

If you have a previous version of Rust installed via rustupthen to upgrade to version 1.63.0 you just need to run the command:

rustup update stable

If you don’t already have rustupthen you can install it with pages on our website, as well as detailed release notes for 1.63.0 on GitHub.

If you want to help us test future releases, you can use beta (rustup default beta) or nightly (rustup default nightly) channel. Please, inform about all the errors you encountered.

What is stabilized in 1.63.0

Added streams to the standard library that are guaranteed to terminate at the end of a scope. The migration of all editions of the language to NLL has been completed. Added new types to work with operating system file descriptors.

Scope Threads

Rust code could start new threads with std::thread::spawn since version 1.0, but this function is limited by the lifetime 'static in closure. Roughly speaking, this means that threads must currently own any arguments passed to their closure; you can’t stream borrowed data. In cases where threads are expected to complete by the end of the function (by callingjoin()), this is not strictly necessary and may require workarounds such as placing data in Arc.

Beginning with 1.63.0, the standard library adds scoped threads that allow you to spawn a thread that borrows from a local stack frame. API std::thread::scope provides the necessary guarantee that any spawned threads will complete before they return, allowing safe data borrowing. Here is an example:

let mut a = vec![1, 2, 3];
let mut x = 0;

std::thread::scope(|s| {
    s.spawn(|| {
        println!("hello from the first scoped thread");
        // Вы можете заимствовать `a` здесь.
        dbg!(&a);
    });
    s.spawn(|| {
        println!("hello from the second scoped thread");
        //  Вы так же можете изменить заимствованную переменную `x` здесь
        // потому что нет других потоков которые ёё используют.
        x += a[0] + a[2];
    });
    println!("hello from the main thread");
});

// После переменные снова вам доступны и вы можете их изменять:
a.push(4);
assert_eq!(x, a.len());

Ownership for raw file descriptors (I/O security)

Previously, Rust code working with platform APIs that accepted raw file descriptors (on Unix-style platforms) or handles (on Windows) usually worked directly with a platform-specific handle representation (e.g., c_int or alias RawFd). For Rust bindings to such native APIs, the type system was unable to code whether the API would take ownership of the file descriptor (e.g. close) or simply borrows it (for example, dup).

Rust now provides wrapper types like BorrowedFd and OwnedFdwhich are marked as #[repr(transparent)]which means that external bindings extern "C" can directly use these types to encode ownership semantics. For a complete list of wrapper types stabilized in version 1.63, see the Stabilized APIs section. They are currently available on cfg (unix), Windows and WASI platforms.

We recommend that new APIs use these types instead of previous type aliases (for example RawFd).

`::<> for generalizations in functions with impl Trait

Notation ::<> can be used to specify the parameter type (fn foo<T: Copy>) even when the argument type is specified impl Trait. But specifying the type for the argument impl Trait through this notation is still not possible.

Completing the migration of non-lexical lifetimes

As described in this post, we have completely removed the previous borrowing lexer from rustc for all editions, and the new non-lexical borrowing analyzer is fully operational. Now the borrow parser does not affect the output of rustc. This does not change the behavior of the programs, but it completes a long migration that began with the stabilization of NLL in the 2018 edition, which brings all the benefits of the new borrowing analyzer to all editions of Rust. For most users, this change will look like a slightly improved diagnosis of some borrowing errors, but it won’t affect the kind of code they can write.

You can learn more about non-lexical lifetimes in this section of the 2018 edition announcement.

Stabilized APIs

The following trait methods and implementations have been stabilized:

The following APIs are now available for use in const context:

Other changes

Syntax, Cargo package manager and clippy analyzer also undergone some changes.

Members 1.63.0

Many people have come together to create Rust 1.63.0. We wouldn’t have made it without you. Thank you!

From translators

For any questions about the Rust language, we can help you with Russian-language Telegram chat or in a similar chat for newbie questions. If you have questions about translations or want to help with them, please contact translators chat. You can also support us on OpenCollective.

This article was jointly translated by andreevlex and funkill.

Similar Posts

Leave a Reply