为什么不在if/else的末尾添加分号(;)? [英] Why don't we add a semicolon (;) at the end of if/else?
本文介绍了为什么不在if/else的末尾添加分号(;)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Rust中,我注意到除2种语句外,所有内容都是一个表达式.每个添加;
的表达式都将成为一条语句. Rust的语法希望语句跟随其他语句.
In Rust, I have noticed that everything is an expression except 2 kinds of statements. Every expression that adds ;
will become a statement. Rust's grammar wants statements to follow other statements.
那么为什么不在if
/else
表达式"的末尾添加;
?这也是一个表达式,所以我们为什么不这样做:
So why don't we add ;
at the end of an if
/ else
"expression"? This is also an expression, so why don't we do this:
if true {
println!("true");
} else {
println!("false");
};
推荐答案
我想是因为它是Java或Nginx-Conf等其他语言中的块表达式,所以只能在语句之后而不是在块之后设置分号.
I guess because it is a block expression in other languages like Java or Nginx-Conf a semicolon is only set after statements and not after blocks.
这篇关于为什么不在if/else的末尾添加分号(;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文