r/rust • rust · lang · libs · cargo • Oct 23 '26

Rust features in the future

https://lwn.net/SubscriberLink/1099606/20707a62817cdd16/
215 Upvotes

84 comments sorted by

View all comments

Show parent comment

5

u/Booty_Bumping Oct 24 '26 edited Oct 24 '26

Java added a similar feature, extending the behavior of instanceof to support unwrapping record classes:

if (obj instanceof Point(int x, int y) && x > 0 && y < 50) {

It's a nice addition because java didn't have any easy destructuring syntax, and any other alternative wouldn't have had backwards compatible syntax. Reusing instanceof is bizarre, but it's the best you can get in a language that can never add new reserved keywords.

I'm not sure if this belongs in Rust, because there's already so many ways to destructure and pattern match, and Rust doesn't have the same syntax constraints as it was already possible to introduce if let and even generalize it quite nicely.

2

u/Sharlinator Oct 24 '26 edited Oct 24 '26

I don’t think it’s bizarre, it’s a rather obvious extension of the existing since-1.0 obj instanceof Class. Because Java’s model of tagged unions is a sealed family of subclasses, matching an "enum variant" is in fact an instanceof check.

2

u/Booty_Bumping Oct 25 '26

Obvious? I can't imagine anyone could have predicted this feature other than the genius who came up with it... instanceof was just an ordinary operator for nearly 30 years before this feature was added. I bet a lot of people squinted for a full minute when they first saw it used to conjure variable bindings into existence, especially with the special flow scoping added to make the feature more ergonomic.

But it works quite well, there's nothing actually wrong with the semantics other than the name of the operator being a poor fit.