E0474: Linear field dropped by destructure
| Code | Category | Stability |
|---|---|---|
E0474 | Struct and enum | Permanent |
Explanation
A field access on a struct declared linear consumes and destructures the smallest enclosing declared-linear place. Rue rejects that access when the residue contains another linear value, because destroying the residue would silently discard its must-consume obligation. A struct that is only linear by infection instead uses ordinary partial-move rules.
Likely cause
Code projects one field from a declared-linear struct while a sibling field, nested field, or residual array element also carries a linear value. Consume the whole declared-linear value, or restructure the operation so every linear component is explicitly consumed.
Examples
Destructure a declared-linear value with linear residue
linear struct Token { value: i32 }
linear struct Holder { token: Token, tag: i32 }
fn main() -> i32 {
let holder = Holder { token: Token { value: 1 }, tag: 41 };
holder.tag
}
Consume the declared-linear value explicitly
linear struct Token { value: i32 }
linear struct Holder { token: Token, tag: i32 }
fn main() -> i32 {
let holder = Holder { token: Token { value: 1 }, tag: 41 };
@drop(holder);
0
}