Bitcoin

Introducing Rust 1.86.0: Trait Upcasting, HashMaps, and More

The Rust team is pleased to announce a new version of Rust, 1.86.0. Rust is a programming language allowing everyone to create reliable and effective software.

If you have a previous version of Rust installed via rustupYou can get 1.86.0 with:

$ rustup update stable

If you don't already have it, you can get rustup From the appropriate page of our website and consult the detailed version notes for 1.86.0.

If you want to help us by testing future versions, you might consider updating locally to use the beta channel (rustup default beta) or the night chain (rustup default nightly). Please point out all the bugs you may meet!

What is in 1.86.0 stable

Upcasting trait

This version includes a long -awaited functionality – the possibility of obtaining upy draft objects. If a line has a superitrait, you can force a reference to this line object to a reference to an object of line of the superitrait:

trait Trait: Supertrait {}
trait Supertrait {}

fn upcast(x: &dyn Trait) -> &dyn Supertrait {
    x
}

The same would work with any other type of pointer (intelligent), like Arc -> Arc Or *const dyn Trait -> *const dyn Supertrait.

Previously, this would have required a bypass solution in the form of a upcast Method in the Trait himself, for example fn as_supertrait(&self) -> &dyn SupertraitAnd it would only work for a type of reference / pointer. Such bypass solutions are no longer necessary.

Note that this means that raw pointers to draft objects transport a non -trivial invariant: “flee” a raw pointer to a draft object with a non -valid vable in a safe code can lead to indefinite behavior. It is not yet decided if the creation of a pointer so raw temporarily in well -controlled circumstances causes an unlikely immediate behavior, the code should therefore refrain from creating such pointers in all conditions (and Miri strengthens it).

Upcasting features can be particularly useful with the Any line, because it allows the updating of your line object dyn Any to call AnyMethods down, without adding line methods or using external boxes.

use std::any::Any;

trait MyAny: Any {}

impl dyn MyAny {
    fn downcast_ref(&self) -> Option<&T> {
        (self as &dyn Any).downcast_ref()
    }
}

You can find out more about the Upcasting features in the Rust reference.

HashMapS and the slices now support the indexing of several elements

The loan verifier prevents simultaneous use of references obtained from repeated calls to get_mut Methods. To securely support this model, the standard library now provides a get_disjoint_mut assistant on the slices and HashMap To simultaneously recover mutable references from several elements. See the following example drawn from the API documents of slice::get_disjoint_mut::

let v = &mut [1, 2, 3];
if let Ok([a, b]) = v.get_disjoint_mut([0, 2]) {
    *a = 413;
    *b = 612;
}
assert_eq!(v, &[413, 2, 612]);

if let Ok([a, b]) = v.get_disjoint_mut([0..1, 1..3]) {
    a[0] = 8;
    b[0] = 88;
    b[1] = 888;
}
assert_eq!(v, &[8, 88, 888]);

if let Ok([a, b]) = v.get_disjoint_mut([1..=2, 0..=0]) {
    a[0] = 11;
    a[1] = 111;
    b[0] = 1;
}
assert_eq!(v, &[1, 11, 111]);

Allow safety functions to be marked with the #[target_feature] attribute.

Previously only unsafe Functions could be marked with the #[target_feature] Attribute because it is not solid to call such functions without the target functionality being activated. This version stabilizes the target_feature_11 functionality, allowing on functions to mark with the #[target_feature] attribute.

The sure functions marked with the target functionality attribute cannot be called safely from other marked functions of the target functionality attribute. However, they cannot be transmitted to the functions accepting generics delimited by the Fn* The lines and the support only are forced to functions of functions inside marked functions with the target_feature attribute.

Within the unmarked functions of the target functionality attribute, they can be called inside a unsafe Block, but it is the appellant's responsibility to ensure that the target functionality is available.

#[target_feature(enable = "avx2")]
fn requires_avx2() {
    // ... snip
}

#[target_feature(enable = "avx2")]
fn safe_callsite() {
    // Calling `requires_avx2` here is safe as `safe_callsite`
    // requires the `avx2` feature itself.
    requires_avx2();
}

fn unsafe_callsite() {
    // Calling `requires_avx2` here is unsafe, as we must
    // ensure that the `avx2` feature is available first.
    if is_x86_feature_detected!("avx2") {
        unsafe { requires_avx2() };
    }
}

You can check the target_features_11 RFC for more information.

Rebaches the statements according to which pointers are non -zero when necessary for solidity

The compiler will now insert the debugging assertions according to which a pointer is not zero on readings and non -zero scriptures, and also when the pointer is reprimanded in a reference. For example, the following code will now produce non-subsequent panic when debugging statements are activated:

let _x = *std::ptr::null::();
let _x = &*std::ptr::null::();

Trivial examples like this have produced a warning from Rust 1.53.0, the new execution check will detect these scenarios regardless of its complexity.

These statements only take place when debugging claims are activated, which means that they should not be invoked for solidity. This also means that the outbuildings that have been compiled with disabled debugging assertions (for example the standard library) will not trigger assertions even when called by code with activated debugging assertions.

Do missing_abi Lint warns by default

Omit the abi in external blocks and functions (for example extern {} And extern fn) will now result in a warning (via the missing_abi plush). Omitting the Abi after extern The keyword has always implicitly led to "C" Abi. It is now recommended to explicitly specify the "C" Abi (for example extern "C" {} And extern "C" fn).

You can check the explicit ABI Extern RFC for more information.

Target depreciation warning for 1.87.0

The level 2 target i586-pc-windows-msvc will be deleted in the next version of Rust, 1.87.0. His difference for the most popular i686-pc-windows-msvc is that it does not require the management of the SSE2 instruction, but Windows 10, the minimum version of the operating system of all windows targets (except win7 targets), requires SSE2 instructions itself.

All users currently targeting i586-pc-windows-msvc should migrate to i686-pc-windows-msvc Before the 1.87.0 release.

You can check the major change proposal for more information.

Stabilized API

  • {float}::next_down
  • {float}::next_up
  • <[_]>::get_disjoint_mut
  • <[_]>::get_disjoint_unchecked_mut
  • slice::GetDisjointMutError
  • HashMap::get_disjoint_mut
  • HashMap::get_disjoint_unchecked_mut
  • NonZero::count_ones
  • Vec::pop_if
  • sync::Once::wait
  • sync::Once::wait_force
  • sync::OnceLock::wait

These APIs are now stable in Contexts Const:

  • hint::black_box
  • io::Cursor::get_mut
  • io::Cursor::set_position
  • str::is_char_boundary
  • str::split_at
  • str::split_at_checked
  • str::split_at_mut
  • str::split_at_mut_checked

Other changes

Discover everything that has changed in rust, cargo and clippy.

Contributors to 1.86.0

Many people have come together to create Rust 1.86.0. We could not have done it without all of you. THANKS!


Credits: the Rust Liberation team

Also published here

Photo by Nik Shuliahin 💛💙 on Unsplash

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblocker Detected

Please consider supporting us by disabling your ad blocker