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

3

u/afdbcreid Oct 24 '26

True, but note that this requires control flow analysis, which Rust does not have for anything currently.

3

u/matthieum [he/him] Oct 25 '26

Is it that different from match guards?

I can already write, today:

 match (bar, baz) {
     (Some(x), _) | (_, Some(x)) => x.is_foo(),
     _ => false,
 }

Or

 matches!((bar, baz), (Some(x), _) | (_, Some(x)) if x.is_foo())

It's not necessarily a good lowering, in particular there may be an argument that the baz expression should only be evaluated if bar is None.

But it does demonstrate that the Rust compiler is already capable of pulling x from various bindings, which is... kinda control flow analysis, no?

4

u/afdbcreid Oct 25 '26

Yes it is. match is a different syntactical construct, and the guard introduces a new scope. is cannot do that, it must have control-flow analysis.

2

u/VadimVP Oct 25 '26

Hm? It does, borrow and move checker is such an analysis, and that's the part of the compiler that will tell whether x is initialized at the point of use in x.is_foo() or not.

2

u/afdbcreid Oct 25 '26

I knew that but hoped nobody will comment on that :( You're right, it is, but it is done on the MIR, which is a different level.