New Rust Features Include Generic Associated Types (GATs) After Six Years of Waiting


The latest stable version of Rust, 1.65.0, includes Generic Associated Types (GATs) – the ability to declare lifetime, type, and const generics on associated types. “It’s hard to put into words how useful this can be,” writes the official Rust blog.

A previous post pointed out that “there were a fair number of changes that had to be made to the compiler to get GATs to work”, noting that the request for comments for this feature was first opened in 2016.

And the Rust typography team also created a blog post with more details:
Note that this only expands where you can put generics: for example, you may already have generics on standalone type aliases and on functions in traits. Now you can just have generics on type aliases in traits (which we just call related types)…

In general, GATs provide a fundamental basis for a wide range of models and APIs. “we want the API to look like X, but for that we need GAT” (or see this comment which already has some). If you’re interested in how GATs allow a library to perform copyless analysis, resulting in an almost tenfold performance increase, you might be interested in a blog post about it by Niko Matsakis .

Overall, although you won’t need to use GATs directly, it’s very possible that the libraries you’ll be using GATs internally or publicly for usability, performance, or simply because it’s the only way the implementation works…. [A]All the different people involved in achieving this stabilization deserve the warmest thanks. As said before, it’s been 6.5 years and it couldn’t have happened without everyone’s support and dedication.
Rust 1.65.0 also contains otherwise statements – a new kind of let statement “with a refutable pattern and a divergent else block that executes when that pattern doesn’t match,” according to the posting announcement.

And it highlights another new feature:

Simple block expressions can now be labeled as a Pause target, ending that block early. It may look a bit like a go to statement, but it’s not an arbitrary jump, only from inside a block to its end. It was already possible with loop blocks, and you may have seen people write loops that always run only once, just to get a label Pause.

Now there’s a language feature specifically designed for that! Labeled Pause can also include an expression value, just like with loops, leaving a multi-statement block to have an early “return” value.

Source link


Comments are closed.