-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Explain when temporary 'anonymous' values will be dropped #850
Comments
I think this isn't really so much "when temporaries will get dropped", but the rule that the value resulting from the last expression in a block is returned from the block implicitly. Given that this is documented in the reference, and we're attempting to cover the most common cases people need to be productive rather than trying to be comprehensive, I'm currently inclined not to discuss this in the book, but only just. @steveklabnik what do you think? The other potential issue is that if we want to discuss this, where would we? And has that chapter been frozen yet? |
@carols10cents I don't understand what you mean by
So I prepared a simple example to more clearly illustrate what I mean: struct Goodbye;
impl Goodbye {
fn new() -> Self { Goodbye }
fn get(&self) -> bool { true }
}
impl Drop for Goodbye {
fn drop(&mut self) {
println!("Dropping");
}
}
fn main() {
let x = match Goodbye::new().get() {
true => {
println!("In match");
1
},
false => 0
};
} What would be printed first? "Dropping" or "In match"? I think it depends on when the temporaries get dropped. (Edit for those unfamiliar with the language: this is deterministic. "In match" is always printed first.) A couple of months ago, I would have been surprised when I saw which one gets printed first. Now in general, Rust is a language with very few surprises. That's why I love it and what powers the fearless in "fearless concurrency". You don't need know every detail of the language nor be aware of every pitfall in the language; if you get it wrong, the Rust compiler will catch you (in general). However, if your don't know when temporaries get dropped and you get it wrong, the compiler doesn't catch you (at least for now) and you can end up with a bug. This is why I believed "when temporaries get dropped" is important enough to know and should be included in The Book. (plot twist) While preparing the example I gave you above, I discovered a difference between So I honestly don't know anymore how to proceed with this. Maybe it is enough to write something like
But that's too vague. I don't know. I hope you have an idea how to solve this. Otherwise we can close it until someone comes up with a better solution. |
Yes, I don't think we should cover this. Not only because it's pretty rare, but because it's also sort of changing, see stuff like rust-lang/rfcs#2025 |
Ok, closing for now! |
After reading reading The Book, readers get an idea when named variables are being dropped. For temporary values (which I tend to call 'anonymous values') however, I couldn't find any part in The Book that indicates when these are dropped.
When I didn't know when temporaries will be dropped, I used my intuition to fill the gap in my knowledge. Because my intuition was incorrect, I could have written code that does not behave as I would expect. An example of how this can lead to bugs can be found in rust-lang/rust#37612. For this reason, I think it is worth mentioning when temporaries are dropped.
The reference includes a section explaining when temporary values will be dropped.
The text was updated successfully, but these errors were encountered: