Put somthing otherthan {} or "if" after "else"


So I just started learning rust and I am curious. Why we can not put any expression/statement after an else statement?

Correct me if I am wrong (which I know I am ! Right now I am trying to understand statements and expressions ) I think it would be better if we were able to put anything we want after an else statement INCLUDING a {} or an if statement
Like this code

if false {

} else match coin {
    Coin::Penny => 1,
    Coin::Nickel => 5,
    Coin::Dime => 10,
    Coin::Quarter => 25,

  1.  if false {
     } else

2. Consistency.


RFC 1712 covered else match, closed after a lengthy discussion.


Thanks for the link.I just finished reding it!
But that is not exactly what I meant.
I am saying that compiler should be expecting an expression or statement after else. wich could be any thing.
not just things ( if and {}).
Thay said that it was too difficult and not neccesary.I am not a developer of rust compiler but wouldn’t it be easier to tell the compiler that it can expect any kind of expression rather than just two specefics?

I think this way codes can be more neat and clean.
But that was just an idea. we cloud even not have the else if and write it inside of the {}


It’s an explicit design decision that if should always use braces to clearly mark its block. This avoids ambiguity like dangling else:

// in C:
if (foo)
    if (bar)
else abort(); // which `if` does this belong to?

Allowing else if is a concession to a common pattern, which can still be parsed unambiguously. As you saw, the RFC for else match also discussed extending this idea to any block-like keyword.

If we allowed any else expression or else statement without braces, then we’d have the inconsistency that braces are required on if but not on else, and we’d probably get complaints about that even more. There may also be some parsing ambiguity with this, but I’m not sure.


Rust is a curly-brace language. We’re never going to have braceless blocks like Python. It leads to all kinds of terrible problems in languages that allow it and don’t mandate whitespace.

I agree with you that else match (since match MUST be exhaustive, we know that the total if/else stack is exhaustive) would be cool and a natural extension of else if; I can’t believe that never occurred to me.

if cond and else introduce new scopes, which MUST be delimited by braces. else if cond { block } is a common shorthand and is essentially equal to else { if cond { block } }. Rust doesn’t expect an expression or a statement after conditionals; it expects a block scope. The only time Rust permits brace elision, AFAIK, is on match arms (a decision with which, incidentally, I massively disagree, but at least arms are mandated to have terminating commas if the braces are elided); doing so on if/else is a proven recipe for disaster.