Skip to content

Commit c4ebffe

Browse files
chorman0773ehuss
authored andcommittedJan 30, 2025
Add identifier syntax to if-expr and literal-expr
1 parent aae16d5 commit c4ebffe

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
 

‎src/expressions/if-expr.md

+30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# `if` and `if let` expressions
22

3+
r[expr.if]
4+
35
## `if` expressions
46

7+
r[expr.if.syntax]
58
> **<sup>Syntax</sup>**\
69
> _IfExpression_ :\
710
> &nbsp;&nbsp; `if` [_Expression_]<sub>_except struct expression_</sub> [_BlockExpression_]\
@@ -10,13 +13,26 @@
1013
> | _IfExpression_
1114
> | _IfLetExpression_ ) )<sup>\?</sup>
1215
16+
r[expr.if.intro]
1317
An `if` expression is a conditional branch in program control.
1418
The syntax of an `if` expression is a condition operand, followed by a consequent block, any number of `else if` conditions and blocks, and an optional trailing `else` block.
19+
20+
r[expr.if.constraint]
1521
The condition operands must have the [boolean type].
22+
23+
r[expr.if.condition-true]
1624
If a condition operand evaluates to `true`, the consequent block is executed and any subsequent `else if` or `else` block is skipped.
25+
26+
r[expr.if.else-if]
1727
If a condition operand evaluates to `false`, the consequent block is skipped and any subsequent `else if` condition is evaluated.
28+
29+
r[expr.if.else]
1830
If all `if` and `else if` conditions evaluate to `false` then any `else` block is executed.
31+
32+
r[expr.if.result]
1933
An if expression evaluates to the same value as the executed block, or `()` if no block is evaluated.
34+
35+
r[expr.if.type]
2036
An `if` expression must have the same type in all situations.
2137

2238
```rust
@@ -39,6 +55,9 @@ assert_eq!(y, "Bigger");
3955

4056
## `if let` expressions
4157

58+
r[expr.if.let]
59+
60+
r[expr.if.let.syntax]
4261
> **<sup>Syntax</sup>**\
4362
> _IfLetExpression_ :\
4463
> &nbsp;&nbsp; `if` `let` [_Pattern_] `=` [_Scrutinee_]<sub>_except lazy boolean operator expression_</sub>
@@ -48,9 +67,16 @@ assert_eq!(y, "Bigger");
4867
> | _IfExpression_
4968
> | _IfLetExpression_ ) )<sup>\?</sup>
5069
70+
r[expr.if.let.intro]
5171
An `if let` expression is semantically similar to an `if` expression but in place of a condition operand it expects the keyword `let` followed by a pattern, an `=` and a [scrutinee] operand.
72+
73+
r[expr.if.let.pattern]
5274
If the value of the scrutinee matches the pattern, the corresponding block will execute.
75+
76+
r[expr.if.let.else]
5377
Otherwise, flow proceeds to the following `else` block if it exists.
78+
79+
r[expr.if.let.result]
5480
Like `if` expressions, `if let` expressions have a value determined by the block that is evaluated.
5581

5682
```rust
@@ -74,6 +100,7 @@ if let _ = 5 {
74100
}
75101
```
76102

103+
r[expr.if.let.else-if]
77104
`if` and `if let` expressions can be intermixed:
78105

79106
```rust
@@ -90,6 +117,7 @@ let a = if let Some(1) = x {
90117
assert_eq!(a, 3);
91118
```
92119

120+
r[expr.if.let.desugaring]
93121
An `if let` expression is equivalent to a [`match` expression] as follows:
94122

95123
<!-- ignore: expansion example -->
@@ -111,6 +139,7 @@ match EXPR {
111139
}
112140
```
113141

142+
r[expr.if.let.or-pattern]
114143
Multiple patterns may be specified with the `|` operator. This has the same semantics as with `|` in `match` expressions:
115144

116145
```rust
@@ -125,6 +154,7 @@ if let E::X(n) | E::Y(n) = v {
125154
}
126155
```
127156

157+
r[expr.if.let.restriction]
128158
The expression cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
129159
Use of a lazy boolean operator is ambiguous with a planned feature change of the language (the implementation of if-let chains - see [eRFC 2947][_eRFCIfLetChain_]).
130160
When lazy boolean operator expression is desired, this can be achieved by using parenthesis as below:

0 commit comments

Comments
 (0)
Please sign in to comment.