Rust ; {} and indent

https://lang-team.rust-lang.org/frequently-requested-changes.html#fundamental-changes-to-rust-syntax

5 Likes

Thank you very much for sending the book with specific answers from the authors)

Rust's lack of ; is due to expression-oriented nature of rust. For example, code like this is allowed:

let a = {
    let x = 3;
    let b = f(&x);
    b
};

Even assignments can be used as expressions, while returning () as it can actually be useful:

let mut y = '3';
match Some('x') {
    Some('x') => y = 'x',
   None => y = '\0',
    _ => y = '8',
}

Here y = 'x' is an expression of type () and this allows it to be used in match arms. Even more, {} is also an expression with the type of last expression, so here:

match 'x' {
    _ => { 3 }
}

{ 3 } is a block, which is also an expression, and has return type i32. You can also write

match 'x' {
    _ => { let b = 3; b }
}

Or even

match 'x' {
    _ => 'my_block: { let b = 3; break 'my_block b /*optionaly ';'*/ }
}
1 Like

Thanks for helping me clarify the meaning of ;

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.