E0494: Linear value overwritten through inout
| Code | Category | Stability |
|---|---|---|
E0494 | Struct and enum | Permanent |
Explanation
An inout parameter aliases the caller's initialized storage. Assigning a linear value to that parameter or a linear place rooted at it would implicitly drop the caller's live value. Because a by-reference binding cannot be moved out to establish empty storage, this assignment is always rejected.
Likely cause
A function tries to replace a linear inout argument. Pass ownership by value and return a replacement, mutate non-linear fields in place, or otherwise arrange for the caller to consume the old value explicitly rather than overwriting it through the loan.
Examples
Overwrite a linear inout parameter
linear struct Token { value: i32 }
fn replace(inout token: Token) { token = Token { value: 42 }; }
fn main() -> i32 {
let mut token = Token { value: 1 };
replace(inout token);
@drop(token);
0
}
Mutate a non-linear field through inout
linear struct Token { value: i32 }
fn update(inout token: Token) { token.value = 42; }
fn main() -> i32 {
let mut token = Token { value: 1 };
update(inout token);
@drop(token);
0
}