-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document lifetime of temporaries in conditions of if and while-expr #94
Conversation
Explain that temporaries created in the condition expression of an if or if/else are dropped immediately after the condition expression.
Explain that temporaries created in the loop conditional expression of a while expression are freed immediately after the loop conditional expression.
src/expressions.md
Outdated
When a temporary rvalue is being created that is assigned into a `let` | ||
declaration, however, the temporary is created with the lifetime of the | ||
enclosing block instead, as using the enclosing statement (the `let` | ||
A first exception is when a temporary value is created in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The phrasing here seems odd and opens the possibility that there are more than two exceptions. Perhaps a paragraph stating "There are two exceptions to this rule." prior to the exceptions listings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right: it is odd. I don't know if there are still some exceptions missing, however.
Can someone confirm that there are no other exceptions missing? Then we can add "There are two exceptions to this rule."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue that this is not an exception - the only exception is the let
rule. It's just that the conditional of an if
or while
are counted as a single statement for the purposes of the rule.
Also, side note, you don't have to do the whole taking a reference thing in order to create a temporary object. If an rvalue is not assigned to an lvalue, or is not used to construct an object, then a temporary object is automatically created.
Reformulate text so that dropping of temporaries created in condition expression of an `if` or `if`/`else` or in the loop conditional expression of a `while` is now part of the rule, not an exception. Revert the paragraph concerning the `let` expression to its original form. Move examples. Include an example of a temporary without borrowing.
Thanks! |
Explain that temporaries created in the condition expression of an
if
orif
/else
expression are dropped immediately after the condition expression. Similar forwhile
. See rust-lang/rust#12033