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

Explain when temporary 'anonymous' values will be dropped #850

Closed
PieterPenninckx opened this issue Jul 26, 2017 · 4 comments
Closed

Explain when temporary 'anonymous' values will be dropped #850

PieterPenninckx opened this issue Jul 26, 2017 · 4 comments

Comments

@PieterPenninckx
Copy link

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.

@carols10cents
Copy link
Member

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?

@PieterPenninckx
Copy link
Author

PieterPenninckx commented Jul 31, 2017

@carols10cents I don't understand what you mean by

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.

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 if and match and I opened an issue for that (rust-lang/rust#43565). It turns out that there is indeed a difference and it's not a bug: temporaries in the condition-expression of an if get dropped sooner. Explaining all this in The Book seems too much (the general rule for temporaries + the exception for temporaries in the condition-expression of an if-expression + maybe some other exceptions).

So I honestly don't know anymore how to proceed with this. Maybe it is enough to write something like

If it's important when precisely a value will be dropped, store it directly or indirectly in a variable, so that it is clear from reading the code when the value will be dropped.

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.

@steveklabnik
Copy link
Member

@steveklabnik what do you think?

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

@carols10cents
Copy link
Member

Ok, closing for now!

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

No branches or pull requests

3 participants