Destructor

1

Drop in Depth

6 minute

Earlier we used Drop as the first replacement for C cleanup paths: when a value leaves scope, its resource is released automatically.

That already replaces many goto cleanup patterns. But when writing library code that wraps file handles, connections, locks, temporary directories, or FFI handles, “scope exit releases the value” is not enough.

C programmers still need answers to practical questions:

  • What is the exact cleanup order?
  • How do you release early?
  • What if cleanup can fail?
  • Which struct field is dropped first?
  • Can Drop move a field out?
  • How should an FFI handle be wrapped?

These are the parts of Drop that matter in real engineering code.

Read More