Should I end an expression with ; inside a loop?

match, if, loop, and other expressions that have {} blocks are treated specially by the Rust compiler. When these expressions occur as expression statements, that is, not as part of a larger expression, and the type of the expression that has the block is (), you do not have to put ; after it to separate it from a following statement.

This is not the case for expression statements without blocks, which always must be separated from the following statement (if one exists) by ;, even if their type is ().

Because this rule exists, it is usual in Rust not to put ; after match, if, unsafe, etc. when they are used only for side effects, or after for and while loops (which are always used only for side effects, since they always return ()).

That said, both rustfmt and Clippy seem to be fine with the extra ;, so if you prefer it there for aesthetic reasons, you're unlikely to offend anyone by using it.

Tags:

Rust