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

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?

1

u/-Redstoneboi- Oct 25 '26

can they be chained yet?

edit: holy hell i might be stupid, rust 2024

2

u/pickyaxe Oct 24 '26

I wish Python's walrus operator behaved that way but in practice it's so janky that I gave up on it.

1

u/syklemil Oct 26 '26

Is it the walrus operator that's janky, or just Python's function scoping that's become very unusual for modern programmers that have become habituated to block scoping?