const Thing = struct { detached: bool = false, fn deinit(self: *Thing) void { if (!self.detached) { doTheCleanup(); } } fn consume(self: *Thing) void { self.detached = true; } }; test "usage code" { var thing = functionThatReturnsAThing(); defer thing.deinit(); // ... thing.consume(); }