E0456: Move field out of destructor type
| Code | Category | Stability |
|---|---|---|
E0456 | Struct and enum | Permanent |
Explanation
Rue rejected a move of a field out of a value whose type has a user-defined destructor. The destructor and the automatic field cleanup that follows it must receive the whole value, so moving one field away would let them observe or drop moved-from storage. Moving the whole value is still allowed, as are borrow and inout access to its fields.
Likely cause
A field with move semantics was assigned to a new binding, returned, or passed to a by-value parameter while one of its enclosing values has a user-defined destructor. Keep the field in place and borrow it, read only Copy data from it, or move the whole enclosing value to transfer ownership together with its destructor.
Examples
Move a field away from a destructor-bearing value
struct Payload { value: i32 }
struct Resource { payload: Payload }
drop fn Resource(self) { @dbg(self.payload.value); }
fn main() -> i32 {
let resource = Resource { payload: Payload { value: 42 } };
let payload = resource.payload;
payload.value
}
Read Copy data without moving the field
struct Payload { value: i32 }
struct Resource { payload: Payload }
drop fn Resource(self) { @dbg(self.payload.value); }
fn main() -> i32 {
let resource = Resource { payload: Payload { value: 42 } };
resource.payload.value
}