-
Notifications
You must be signed in to change notification settings - Fork 30
Always separately bind RAII guards: Use scope instead of explicit drop. #6
Comments
Why is that better? That argument seems a bit circular. |
Let me try to expand my argument: If you use the scope rather than calling the destructor manually, it is impossible to reuse the object after destroying it (it would give a compile time error about an undefined variable). (I don't know whether Rust is clever enough to give a compile time error if you try to use an object after Also, I think it makes the lifetime of the object easier to parse. |
You cannot use a variable after it has been moved. If you could, it would be a massive soundness hole: struct Foo {
a: int
}
impl Drop for Foo {
fn drop(&mut self) {}
}
fn main() {
let foo = Foo { a: 10 };
drop(foo);
println!("{}", foo.a);
}
|
Interesting, so the compiler catches this. I guess the only advantage of having a new scope is then that the lifetime is more obvious. (I also think it looks cleaner, but that is probably bikeshedding.) |
Block-scoping may be the way to go to be more consistent with the way that borrows work, at least until rust-lang/rust#6393 is fixed. |
I do think that explicit nested scopes are the prevailing style. However, once the change to our drop semantics changes to require all control flow joins to have the same set of dropped things (see this comment), then explicit calls to |
I'm not sure whether this discussion is still relevant with current Rust (1.6), but sometimes you don't really know whether you own the lock or have a borrow to it. Using drop explicitly is easier than figuring out the type of a variable. |
N.B. this is no longer the plan of record., so thst argument is moot |
@pijul if all you have is a borrow of a lock, then dropping it (the borrow) will have no effect. So I am not sure what distinction you were drawing there. |
Ok, probably my misunderstanding then. Sorry for the noise. |
I think
is better than
because it uses the scope instead of explicitly calling the destructor.
The text was updated successfully, but these errors were encountered: