Why don't we add a semicolon (;) at the end of the if / else?

In Rust, I noticed that this is all but two kinds of statements. Each expression adding ;

will become an expression. The grammar of rust wants statements to match other statements.

So why don't we add a / " ;

at the end of the expression ? This is also an expression, so why don't we do this:if

else

if true {
    println!("true");
} else {
    println!("false");
};

      

+2


source to share


1 answer


The most common answer to the discussion is that it sounds more like what users from other languages ​​expect, and there is no harm in resolving this syntax (since the result is ()

due to semicolons in the branches).



0


source







All Articles