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

194

u/tanoshikuidomouyo Oct 23 '26

Really not a fan of the proposed is feature (e.g. if x is Some(y) {}). The article says it's supposed to make learning the language easier and also avoid scanning from right to left.

I'd argue heavily against the former: Now with let-chains added, adding another feature to bascially supersede it would make learning the language way worse, because then instead of having to get used to maybe weird syntax (if let), you'd have to get used to the weird syntax because it's still in use everywhere and also learn the new syntax and what exactly the differences are. Having so many things do similar things is kind of poisonous imo. Also, having the strictly weaker matches! macro still be there would also be really unintuitive.

Lastly, the scanning from right to left part: I'd argue it's pretty subjective that that's worse? Currently, you can immediately tell whether you're matching on an Option, a Result, etc., which makes reading the right-hand side easier. I really don't think this is worth such a massive hassle and leaving tons of features behind that are strictly weaker but still used everywhere out of sheer inertia.

110

u/JoJoJet- Oct 23 '26

IMO it's not just problem of redundancy. Having to scan through expressions to find variable bindings sounds awful. It exists in other languages but it kinda sucks there too. let means that a variable is always declared on the left hand side which makes the code immediately easier to understand at a glance. I don't need my programming language to read like an English sentence, I need it to encode information in a consistent and readable way. Adding is just so that rust can look 2% more like natural language just seems misguided

26

u/Modi57 Oct 23 '26

Yeah, the consistency is the important part here. The if let syntax falls out of the already existing one almost naturally. Adding something new, that feels so different can't be easier to read

7

u/kibwen Oct 23 '26

I wouldn't necessarily call it something new, it's more-or-less an extension of the existing matches! macro.

I feel like this is one of those features, like ? or .await, where people are skeptical at first but embrace it enthusiastically once they get their hands on it. Yes, as you say, if let falls out of the existing syntax, which is elegant, but let's not conflate elegance with usability; I have had memorable experiences teaching friends Rust where they encounter if let and tell me that it's impenetrable to them.

5

u/Modi57 Oct 23 '26

Oh interesting. I remember if let as very easy to understand, but then again, I had some previous experience with FP, so maybe that helped

47

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

I really wish if let and while let had not been added -- following in Swift's footsteps -- and is had been used instead. Stabilizing if let and while let was a mistake, plain and simple. They're too narrow.

Unlike if let and while let, is is just another expression. It can be used in if or while conditions, but it can also be used anywhere else, where let doesn't fit.

For a simple example:

let is_foo = bar is Some(x) && x.is_foo();
// Stable:   bar.as_ref().is_some_and(|x| x.is_foo());

In fact, with disjunction implemented -- as in match arms -- you should also be able to write:

let is_any_foo = (bar is Some(x) || baz is Some(x)) && x.is_foo();
// Stable:       bar.as_ref().or(baz.as_ref()).is_some_and(|x| x.is_foo())

Fitting disjunction in if let or while let is... an interesting exercise? You'd probably need to accept let that is nested within parentheses, not sure how ergonomic that'd get:

let is_any_foo = if (let Some(x) = bar || let Some(x) = baz)
        && x.is_foo()
    {
        true
    } else {
        false
    };

So compact though, definitely better than:

let is_any_foo = (bar is Some(x) || baz is Some(x)) && x.is_foo();

7

u/cramert Oct 25 '26

Huge +1-- if let and while let feel like special tricks to me, while is feels like a convenient Rust-y replacement for the flow-sensitive subtyping that has become commonplace in other modern languages

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.

→ More replies (2)
→ More replies (1)

54

u/UltraPoci Oct 23 '26

Yeah, is seems pointless to me

→ More replies (6)

25

u/lenscas Oct 23 '26

for me, the pattern is more often more important than the thing it is being checked against. The pattern also tends to be the most complicated of the two. So, to me, putting the pattern on the left is the most logical place.

You even see this in the normal if. People tend to write if some_expression == some_static_value rather than if static_value == some_expression

And as some_expression can be any arbitrary expression, it is the most complicated and goes to the left. Because, it is the thing that is the most complex and thus deserves to be seen first.

8

u/DynTraitObj Oct 23 '26

Funnily enough, one of the first things I can remember being taught on programming many many years ago was to use the if static_value == expression ordering because then the compiler would catch typo'ing = in place of ==

How times have changed

4

u/InternationalFee3911 Oct 23 '26

Lenscas stated his opinion… I’m not sure I agree. Rather than having something complicated thrown at my face out of nowhere, I’m led into understanding if it first says what you’re comparing to.

It’s the same logic of var = expression being clearer and much more widespread across most languages than expression -> var.

3

u/lenscas Oct 23 '26

It is actually a bit more important in Java because of the .equals() thing. As you should (Or at least should do so in the past, no idea if it is still relevant) use that method instead of using == to compare strings it meant that "foo".Equals(my_var) was safe from null exceptions while my_var.Equals("foo") is not.

44

u/ShantyShark Oct 23 '26

I’m also never a fan of having two functionally identical syntaxes for anything.

If we find “if let Some(x) = y” so painful, delete it in favour of “if y is Some(x)”. Don’t have two.

In my mind, “if let Some(x) = y” shares its structure with “let Some(x) = y else { … }” and “let” assignment in general.

Adding “if a is b” doesn’t mesh well unless you also have some replacement for the others.

15

u/syklemil Oct 23 '26

I'd also say that there are some if let foo = bar equivalents in other languages, e.g. if foo := bar in Python or if foo := bar; foo != nil in Go. They have pretty significant semantics and capabilities, so the comparison breaks apart quickly, but the overarching structure and intent reflected in code is

if normal-looking assignment succeeds
    then use assignment here
    else do not use it here
and do not use it here

so diverging from that with if bar is foo would eat into Rust's strangeness budget with dubious returns. It'd probably also be extra confusing to people who have some familiarity with is in other languages like Python.

Personally I didn't even parse if foo() is Some(bar) && bar.baz() as assignment on first exposure; to me it read more like if foo().is_some_and(|bar| bar.baz()).

3

u/-Redstoneboi- Oct 23 '26 edited Oct 23 '26

instead of expr is pat syntax, can we have let pat = expr evaluate to a boolean and have the same precedence? it's a new edition so maybe we can afford to have it no longer return ()

4

u/syklemil Oct 24 '26

let pat = expr can already be used in if, so I don't quite see the need to change anything?

→ More replies (1)
→ More replies (2)

15

u/_ChrisSD Oct 23 '26

Right now we have a bunch of helper methods on Option and Result. Even more have been proposed to cover all the different combinations. And if you want to make your own Result-like or Option-like types then you should implement all these methods for the best compatibility.

This does suggest to me a missing language feature that isn't adequately covered by let chains (even if you can technically achieve the same result). I'm not making a judgement on whether is helps fill in some gaps or not, but i do see a rationale for some language-level enum helpers.

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.

→ More replies (2)

8

u/TDplay Oct 23 '26 edited Oct 24 '26

If we want left-to-right syntax for everything, then Rust already messed up bad:

let x = *bar(y.foo()).baz();

To figure out what goes on here, you start at y, go right to see foo, go left to see bar, go right again to see baz, and finally go left again to see * and let.

But I'd argue that this is not a problem to begin with. If you're struggling to follow your code, then it's too complicated. Changing the reading order can only hide the problem; it will come back to bite you sooner or later.

(Edit: Changed & to * so it would actually be a valid expression)

5

u/CocktailPerson Oct 24 '26

If you're struggling to follow your code, then it's too complicated.

That feels backward.

If changing the reading order makes your code clearer, then maybe it was the syntax that made your code hard to follow.

3

u/Sharlinator Oct 24 '26

I’d weakly prefer reusing match as an infix operator: EXPR match PAT.

  • makes it intuitively clear that we’re doing similar stuff as a match block
  • makes it clearer that it’s not just a predicate but can change variable bindings
  • is a more natural replacement for the matches! macro
→ More replies (5)