Pre-RFC: Overload Short Curcuits

You could also say that if a { true } else { b } is "extremely clear", but yet we have a || b.

Note that you could also write that as

if let e @ true = a { e } else { b }`

so the extension of a || b for Option to

if let Some(e) = a { e } else { b }

seems like a perfectly reasonable analogue.

Why is that? Is there something rust-specific that would make things worse in rust than elsewhere? In C#, at least, I find ?? clearer and not bug-prone -- and that's even with it starting to get used in intentionally-short-circuiting situations like

this.foo = foo ?? throw new ArgumentNullException(nameof(foo));

If anything, I might guess the opposite, as putting something into a closure means that you lose much of the move-tracking and NLL goodness of rust, so having the compiler understand the control flow here might make it less error-prone than or_else. Not to mention that we know that .or(foo()) vs .or_else(|| foo()) is already a source of bugs for people.

(And, come to think of it, foo || panic("blah") gives a nicer callstack than foo.expect("blah").)

5 Likes