Skip to content

Commit e0563de

Browse files
authored
Merge pull request rust-lang#137 from brauliobz/grammar_expr_if
If and if let grammar
2 parents bbec462 + b451229 commit e0563de

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/expressions/if-expr.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## `if` expressions
44

5+
> **<sup>Syntax</sup>**
6+
> _IfExpression_ :
7+
> &nbsp;&nbsp; `if` [_Expression_]<sub>_except struct expression_</sub> [_BlockExpression_]
8+
> &nbsp;&nbsp; (`else` (
9+
> [_BlockExpression_]
10+
> | _IfExpression_
11+
> | _IfLetExpression_ ) )<sup>\?</sup>
12+
513
An `if` expression is a conditional branch in program control. The form of an
614
`if` expression is a condition expression, followed by a consequent block, any
715
number of `else if` conditions and blocks, and an optional trailing `else`
@@ -31,8 +39,18 @@ let y = if 12 * 15 > 150 {
3139
};
3240
assert_eq!(y, "Bigger");
3341
```
42+
3443
## `if let` expressions
3544

45+
> **<sup>Syntax</sup>**
46+
> _IfLetExpression_ :
47+
> &nbsp;&nbsp; `if` `let` _Pattern_ `=` [_Expression_]<sub>_except struct expression_</sub>
48+
> [_BlockExpression_]
49+
> &nbsp;&nbsp; (`else` (
50+
> [_BlockExpression_]
51+
> | _IfExpression_
52+
> | _IfLetExpression_ ) )<sup>\?</sup>
53+
3654
An `if let` expression is semantically similar to an `if` expression but in
3755
place of a condition expression it expects the keyword `let` followed by a
3856
refutable pattern, an `=` and an expression. If the value of the expression on
@@ -57,3 +75,22 @@ if let ("Ham", b) = dish {
5775
println!("Ham is served with {}", b);
5876
}
5977
```
78+
79+
`if` and `if let` expressions can be intermixed:
80+
81+
```rust
82+
let x = Some(3);
83+
let a = if let Some(1) = x {
84+
1
85+
} else if x == Some(2) {
86+
2
87+
} else if let Some(y) = x {
88+
y
89+
} else {
90+
-1
91+
};
92+
assert_eq!(a, 3);
93+
```
94+
95+
[_Expression_]: expressions.html
96+
[_BlockExpression_]: expressions/block-expr.html

0 commit comments

Comments
 (0)