Rust 1.65 nearly done, including stable Generic Associated Types

Rust 1.65 nearly done, including stable Generic Associated Types

The Rust team is close to releasing version 1.65 of the language, with changes including what is described as “truly a monumental achievement” – generic associated types (GATs) as a stable feature.

The RFC for this feature is dated April 2016, so it has taken over six and a half years to land. According to an earlier post from the Traits Working Group, “it is the most upvoted issue on the Rust repository.” In Rust, a trait is a collection of methods that may be implemented for any data type, somewhat like an interface in other languages but including implementation as well as method signature.

Rust already has generics but “now you can just have generics on type aliases in traits (which we just call associated types),” Rust maintainer Jack Huey explained. This is not just a fine detail. “GATs provide a foundational basis for a vast range of patterns and APIs,” Huey added.

Why has the feature taken so long to stabilize? Huey described some of the issues in August last year, at which point he said that “we have high hopes we can stabilize this feature within the next couple months,” a hope which proved over-optimistic. GATs required a lot of work to Rust internals, including parsing, resolving lifetimes, validating bounds and more, with many of the constituent pull requests (code added to Rust) being “large and with considerable design work behind them,” said Huey.

The less good news is that there are still limitations to GATs. Some GAT code will not compile “even though it should,” Huey said, and “unfortunately, fixing this requires some refactorings to the compiler that isn’t a short-term project.”

The bug is to do with the lifetime of values, and in mitigation the error message from the compiler will now say something like “`array` does not live long enough” which is informative. “We feel the feature is still immensely useful even with [this bug] around,” said Huey. It is better, of course, to have a bug that prevents code compiling, than one that compiles incorrect code. There are some other limitations, and Rust coders are advised to read Huey’s post carefully before diving in.

“I’m ecstatic that this is finally happening! I have multiple projects that have been waiting on this and none of today’s limitations will be issues. Thank you so much to everyone involved!” said one developer.

Version 1.65 includes additional features as documented in the release notes, including a compiler update to LLVM 15. Another newly stabilized feature is let else.

The 1.65 pre-release was made available for testing earlier this week, and full release is scheduled for November 3.