Skip to content

Commit 5eab25f

Browse files
committed
Document desugaring that happen when creating hir
1 parent 320c8e7 commit 5eab25f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/expressions/if-expr.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,24 @@ let a = if let Some(1) = x {
9292
assert_eq!(a, 3);
9393
```
9494

95+
An `if let` expression is equivalent to a `match` expression as follows:
96+
97+
```rust
98+
if let PAT = EXPR {
99+
/* body */
100+
} else {
101+
/*else */
102+
}
103+
```
104+
105+
is equivalent to
106+
107+
```rust
108+
match EXPR {
109+
PAT => { /* body */ },
110+
_ => { /* else */ }, // () if there is no else
111+
}
112+
```
113+
95114
[_Expression_]: expressions.html
96115
[_BlockExpression_]: expressions/block-expr.html

src/expressions/loop-expr.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ while let Some(y) = x.pop() {
8585
}
8686
```
8787

88+
A `while let` loop is equivalent to a `loop` expression containing a `match`
89+
expression as follows.
90+
91+
```rust
92+
'label: while let PAT = EXPR {
93+
/* loop body */
94+
}
95+
```
96+
97+
is equivalent to
98+
99+
```rust
100+
'label: loop {
101+
match EXPR {
102+
PAT => { /* loop body */ },
103+
_ => break,
104+
}
105+
}
106+
```
107+
88108
## Iterator loops
89109

90110
> **<sup>Syntax</sup>**
@@ -118,6 +138,37 @@ for n in 1..11 {
118138
assert_eq!(sum, 55);
119139
```
120140

141+
A for loop is equivalent to the following block expression.
142+
143+
```rust
144+
'label: for PATTERN in iter_expr {
145+
/* loop body */
146+
}
147+
```
148+
149+
is equivalent to
150+
151+
```rust
152+
{
153+
let result = match IntoIterator::into_iter(iter_expr) {
154+
mut iter => 'label: loop {
155+
let next;
156+
match iter.next() {
157+
Some(val) => next = val,
158+
None => break,
159+
};
160+
let PAT = next;
161+
let () = { /* loop body */ };
162+
},
163+
};
164+
result
165+
}
166+
```
167+
168+
> **Note**: that the outer `match` is used to ensure that any
169+
> [temporary values] in `iter_expr` don't get dropped before the loop is
170+
> finished.
171+
121172
## Loop labels
122173

123174
> **<sup>Syntax</sup>**
@@ -210,6 +261,7 @@ and the `loop` must have a type compatible with each `break` expression.
210261
expression `()`.
211262

212263
[IDENTIFIER]: identifiers.html
264+
[temporary values]: expressions.html#temporary-lifetimes
213265

214266
[_Expression_]: expressions.html
215267
[_BlockExpression_]: expressions/block-expr.html

0 commit comments

Comments
 (0)