Skip to content

Commit 9f09763

Browse files
committed
Add a note about block like statements
1 parent acedc32 commit 9f09763

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/statements.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@ declaration until the end of the enclosing block scope.
3636

3737
## Expression statements
3838

39-
An _expression statement_ is one that evaluates an [expression](expressions.html)
40-
and ignores its result. The type of an expression statement `e;` is always
41-
`()`, regardless of the type of `e`. As a rule, an expression statement's
42-
purpose is to trigger the effects of evaluating its expression.
39+
An _expression statement_ is one that evaluates an
40+
[expression](expressions.html) and ignores its result. The type of an
41+
expression statement `e;` is always `()`, regardless of the type of `e`. As a
42+
rule, an expression statement's purpose is to trigger the effects of evaluating
43+
its expression. An expression that consists of only a [block
44+
expression](expressions.html#block-expressions) or control flow expression,
45+
that doesn't end a block and evaluates to `()` can also be used as an
46+
expression statement by omitting the trailing semicolon.
47+
48+
```rust
49+
# let mut v = vec![1, 2, 3];
50+
v.pop(); // Ignore the element returned from pop
51+
if v.is_empty() {
52+
v.push(5);
53+
} else {
54+
v.remove(0);
55+
} // Semicolon can be omitted.
56+
[1]; // Separate expression statement, not an indexing expression.
57+
```

0 commit comments

Comments
 (0)