mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-03-05 08:25:26 +02:00
Clarify distinction between Drop trait and drop function. (#1255)
This clears up some misleading explanation from #246.
This commit is contained in:
parent
ea44e2547b
commit
864bb942c0
@ -31,9 +31,16 @@ fn main() {
|
||||
|
||||
<details>
|
||||
|
||||
* `drop` is called automatically, but it can be called manually like in this example.
|
||||
* If it was called manually, it won't be called at the end of the scope for the second time.
|
||||
* Calling `drop` can be useful for objects that do some work on `drop`: releasing locks, closing files, etc.
|
||||
* Note that `std::mem::drop` is not the same as `std::ops::Drop::drop`.
|
||||
* Values are automatically dropped when they go out of scope.
|
||||
* When a value is dropped, if it implements `std::ops::Drop` then its `Drop::drop` implementation
|
||||
will be called.
|
||||
* All its fields will then be dropped too, whether or not it implements `Drop`.
|
||||
* `std::mem::drop` is just an empty function that takes any value. The significance is that it takes
|
||||
ownership of the value, so at the end of its scope it gets dropped. This makes it a convenient way
|
||||
to explicitly drop values earlier than they would otherwise go out of scope.
|
||||
* This can be useful for objects that do some work on `drop`: releasing locks, closing files,
|
||||
etc.
|
||||
|
||||
Discussion points:
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user