@@ -53,9 +53,13 @@ declaration until the end of the enclosing block scope.
53
53
54
54
An * expression statement* is one that evaluates an [ expression] and ignores its
55
55
result. As a rule, an expression statement's purpose is to trigger the effects
56
- of evaluating its expression. An expression that consists of only a [ block
57
- expression] [ block ] or control flow expression and that does not end a block
58
- can also be used as an expression statement by omitting the trailing semicolon.
56
+ of evaluating its expression.
57
+
58
+ An expression that consists of only a [ block expression] [ block ] or control flow
59
+ expression, if used in a context where a statement is permitted, can omit the
60
+ trailing semicolon. This can cause an ambiguity between it being parsed as a
61
+ standalone statement and as a part of another expression; in this case, it is
62
+ parsed as a statement.
59
63
60
64
``` rust
61
65
# let mut v = vec! [1 , 2 , 3 ];
@@ -68,10 +72,25 @@ if v.is_empty() {
68
72
[1 ]; // Separate expression statement, not an indexing expression.
69
73
```
70
74
75
+ When the trailing semicolon is omitted, the result must be type ` () ` .
76
+
77
+ ``` rust
78
+ // bad: the block's type is i32, not ()
79
+ // Error: expected `()` because of default return type
80
+ // if true {
81
+ // 1
82
+ // }
83
+
84
+ // good: the block's type is i32
85
+ if true {
86
+ 1
87
+ };
88
+ ```
89
+
71
90
[ block ] : expressions/block-expr.html
72
91
[ expression ] : expressions.html
73
92
[ function ] : items/functions.html
74
93
[ item ] : items.html
75
94
[ module ] : items/modules.html
76
95
[ canonical path ] : path.html#canonical-paths
77
- [ implementations ] : items/implementations.html
96
+ [ implementations ] : items/implementations.html
0 commit comments