简体   繁体   English

分号在 Rust 中是可选的吗?

[英]Are semicolons optional in Rust?

Since semicolons are apparently optional in Rust, why, if I do this:由于分号在 Rust 中显然是可选的,为什么,如果我这样做:

fn fn1() -> i32 {    
    let a = 1
    let b = 2
    3
}

I get the error:我收到错误:

error: expected one of `.`, `;`, `?`, or an operator, found `let`
 --> src/main.rs:3:9
  |
2 |         let a = 1
  |                  - expected one of `.`, `;`, `?`, or an operator here
3 |         let b = 2
  |         ^^^ unexpected token

They're not optional.它们不是可选的。 Semicolons modify the behaviour of an expression statement so it should be a conscious decision whether you use them or not for a line of code.分号会修改表达式语句的行为,因此对于一行代码是否使用分号应该是一个有意识的决定。

Almost everything in Rust is an expression. Rust 中的几乎所有东西都是一个表达式。 An expression is something that returns a value.表达式是返回值的东西。 If you put a semicolon you are suppressing the result of this expression, which in most cases is what you want.如果你放了一个分号,你就是在抑制这个表达式的结果,这在大多数情况下是你想要的。

On the other hand, this means that if you end your function with an expression without a semicolon, the result of this last expression will be returned.另一方面,这意味着如果您以不带分号的表达式结束函数,则将返回最后一个表达式的结果。 The same can be applied for a block in a match statement.这同样适用于match语句中的块。

You can use expressions without semicolons anywhere else a value is expected.您可以在任何其他需要值的地方使用不带分号的表达式。

For example:例如:

let a = {
    let inner = 2;
    inner * inner
};

Here the expression inner * inner does not end with a semicolon, so its value is not suppressed.这里的表达式inner * inner不以分号结尾,所以它的值没有被抑制。 Since it is the last expression in the block, its value will be returned and assigned to a .由于它是块中的最后一个表达式,它的值将被返回并赋值给a If you put a semicolon on this same line, the value of inner * inner won't be returned.如果在同一行上放置分号,则不会返回inner * inner的值。

In your specific case, not suppressing the value of your let statement doesn't make sense, and the compiler is rightly giving you an error for it.在您的特定情况下,不抑制let语句的值是没有意义的,并且编译器正确地为您提供了一个错误。 In fact, let is not an expression.事实上, let不是一个表达式。

Semicolons are generally not optional, but there are a few situations where they are.分号通常不是可选的,但在某些情况下它们是可选的。 Namely after control expressions like for , if/else , match , etc.即在forif/elsematch等控制表达式之后。

fn main() {
    let a: u32 = 5;
    if 5 == a {
        println!("Hello!");
    }
    if 5 == a {
        println!("Hello!");
    };
    for x in "World".chars() {
        println!("{}", x);
    }
    for x in "World".chars() {
        println!("{}", x);
    };
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1bf94760dccae285a2bdc9c44e8f658a https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1bf94760dccae285a2bdc9c44e8f658a

(There are situations where you do need to have or not have semicolons for these statements. If you're returning the value from within you can't have a semicolon, and if you're setting a variable to be the value from within you'll need a semicolon.) (在某些情况下,您确实需要为这些语句使用或不使用分号。如果您从内部返回值,则不能使用分号,并且如果您将变量设置为来自内部的值需要一个分号。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM