built-in const, associated type restrictions, automatic lifetime extension

The Rust team is pleased to announce a new version of the language – 1.79.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 update to version 1.79.0 you just need to run the command:

$ rustup update stable

If you don't have it installed yet rustupyou can install it with corresponding page our website and also see detailed release notes on GitHub.

If you'd like to help us test future releases, you can use the beta channel (rustup default beta) or nightly (rustup default nightly). Please, report about all the errors you encounter.

What is stabilized in 1.79.0

Built-in const expressions

Blocks const { ... } are now stable in expression position, allowing constant context to be explicitly introduced without the need for additional declarations (such as defining const elements or associated constants for a trait).

Unlike ordinary constants (const ITEM: ... = ...), built-in constants can be used in generics – their type will be implied, it will not need to be specified explicitly, which will make their use convenient in embedded code fragments. For example in a similar case:

const EMPTY: Option<Vec<u8>> = None;
let foo = [EMPTY; 100];

Now you can write it like this:

let foo = [const { None }; 100];

Notably, this is also true for general contexts where previously a verbose declaration of traits with an associated constant was required:

fn create_none_array<T, const N: usize>() -> [Option<T>; N] {
    [const { None::<T> }; N]
}

This makes the code more concise and easier to read.

See details in reference documentation.

Restrictions on an associated type position

Rust 1.79 stabilizes the associate constraint syntax, which allows us to place constraints at the associated type position within other constraints, i.e. T: Trait<Assoc: Bounds...>. This avoids the need to specify an additional explicit generic type just to constrain the associated type.

This feature allows you to specify restrictions in several places that were previously either not possible or would impose additional unnecessary restrictions on use:

  • IN where – is equivalent to dividing the constraints into two (or more) where. For example, where T: Trait<Assoc: Bound> equivalent where T: Trait, <T as Trait>::Assoc: Bound
  • Supertypes – Constraints specified using the new syntax are applied when the trait is used, not when the constraint is declared. For example trait CopyIterator: Iterator<Item: Copy> {}
  • Bounds of elements of associated types – this allows you to limit nested rigid projections that are associated with the associated type of trait. For example trait Trait { type Assoc: Trait2<Assoc2: Copy>; }
  • Opaque type boundaries (RPIT, TAIT) – they allow you to restrict associated types that are associated with an opaque type that does not have name. For example, impl Iterator<Item: Copy> declares an iterator whose elements implement the trait Copy without specifying the real type name in the restrictions

More information can be found in the stabilization report.

Extending automatic life time

Temporary objects that can be directly referenced in constructs now automatically extend their lifetime in constructs match And if. This occurs in the same way as extending the lifetime of temporary objects in block structures.

For example:

let a = if true {
    ..;
    &temp() // раньше это вызвало ошибку, но теперь время жизни продлено
} else {
    ..;
    &temp() // раньше это вызвало ошибку, но теперь время жизни продлено
};

And

let a = match () {
    _ => {
        ..;
        &temp() // раньше это вызвало ошибку, но теперь время жизни продлено
    }
};

now match previous behavior:

let a = {
    ..;
    &temp() // время жизни продлено
};

This behavior is backwards compatible because these programs were not previously compiled.

Frame pointers are included in standard library assemblies

The standard library supplied by Rust now compiles with -Cforce-frame-pointers=yes, which allows its users to more easily profile their programs. Note that the standard library also comes with debugging information (for example, DWARF), which deleted by default in Cargo release profiles.

Stabilized APIs

The following APIs can now be used in context const:

Other changes

Check everything that has changed in Rust, Cargo And Clippy.

Who worked on 1.79.0

Many people came together to create Rust 1.79.0. We couldn't have done it without you. Thank you!

From translators

They can help you with any questions about the Rust language. Russian-language Telegram chat or similar chat for newbie questions. If you have questions about translations or want help with them, please contact translators chat.

This article was jointly translated by andreevlex, TelegaOvoshey and funkill.

Similar Posts

Leave a Reply

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