Skip to content

Commit

Permalink
Clarify distinction between Drop trait and drop function. (#1255)
Browse files Browse the repository at this point in the history
This clears up some misleading explanation from #246.
  • Loading branch information
qwandor authored Sep 26, 2023
1 parent ea44e25 commit 864bb94
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/traits/drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 864bb94

Please sign in to comment.