Skip to content
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

Merged
merged 3 commits into from
Aug 14, 2017

Conversation

PieterPenninckx
Copy link
Contributor

@PieterPenninckx PieterPenninckx commented Aug 4, 2017

Explain that temporaries created in the condition expression of an if or if/else expression are dropped immediately after the condition expression. Similar for while. See rust-lang/rust#12033

Pieter Penninckx and others added 2 commits August 4, 2017 16:42
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.
@PieterPenninckx PieterPenninckx changed the title Document lifetime of temporaries in condition expression of if-expr Document lifetime of temporaries in conditions of if and while-expr Aug 5, 2017
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
Copy link
Contributor

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?

Copy link
Contributor Author

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."

Copy link

@strega-nil strega-nil Aug 10, 2017

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.
@steveklabnik steveklabnik merged commit 75aee62 into rust-lang:master Aug 14, 2017
@steveklabnik
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants