E0442: Move self out of destructor
| Code | Category | Stability |
|---|---|---|
E0442 | Struct and enum | Permanent |
Explanation
A user-defined destructor moves its whole self value to a new owner. Rue runs a destructor before automatically dropping the value's remaining contents; if self escaped to another owner, that owner would later drop it and re-enter the same destructor.
Likely cause
The destructor passes self by value, returns it, binds it to another owned variable, or invokes a by-value receiver method. Keep self in place and perform cleanup by reading Copy fields, borrowing, or mutating in place instead; moving a non-Copy field is separately rejected.
Examples
Move self into a call from its destructor
struct Resource { value: i32 }
fn consume(resource: Resource) -> i32 { resource.value }
drop fn Resource(self) { consume(self); }
fn main() -> i32 {
let resource = Resource { value: 42 };
resource.value
}
Read self without moving it
struct Resource { value: i32 }
drop fn Resource(self) { @dbg(self.value); }
fn main() -> i32 {
let resource = Resource { value: 42 };
resource.value
}