Skip to content

Commit 9b340a1

Browse files
Pieter PenninckxPieter Penninckx
Pieter Penninckx
authored and
Pieter Penninckx
committed
Document lifetimes of temporaries in while-expression
Explain that temporaries created in the loop conditional expression of a while expression are freed immediately after the loop conditional expression.
1 parent 242118b commit 9b340a1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/expressions.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,21 @@ innermost enclosing statement; the tail expression of a block is considered
8989
part of the statement that encloses the block.
9090

9191
A first exception is when a temporary value is created in the
92-
condition expression of an `if` or an `if`/`else` expression.
93-
In this case, the lifetime ends right after the condition expression.
92+
condition expression of an `if` or an `if`/`else` expression or in the
93+
loop conditional expression of a `while` expression.
94+
In this case, the lifetime ends right after the condition expression or the
95+
loop conditional expression.
9496

95-
Here is an example:
97+
Here are some examples:
9698

97-
- `let x = if foo(&temp()) {bar()} else {bas()};`. The expression `temp()` is
99+
- `let x = if foo(&temp()) {bar()} else {baz()}`. The expression `temp()` is
98100
an rvalue. As the temporary is created in the condition expression
99101
of an `if`/`else`, it will be freed at the end of the condition expression
100-
(in this example before the call to `bar` or `bas` is made).
102+
(in this example before the call to `bar` or `baz` is made).
103+
- `while foo(&temp()) {bar();}`. The temporary containing the return value from
104+
the call to `temp()` is created in the loop conditional expression. Hence it
105+
will be freed at the end of the loop conditional expression (in this example
106+
before the call to `bar` if the loop body is executed).
101107

102108
Another exception is when a temporary rvalue is being created that is assigned
103109
into a `let` declaration. In this case the temporary is created with the

0 commit comments

Comments
 (0)