E0428: Mutate borrowed value
| Code | Category | Stability |
|---|---|---|
E0428 | Struct and enum | Permanent |
Explanation
Code attempts to mutate storage held under a shared loan, such as a borrow parameter or a collection borrowed by for. The restriction covers the borrowed binding itself and every field or element projected from it, including passing any such place inout.
Likely cause
Code assigns through a shared-borrowed parameter or mutates a collection while iterating it, possibly by forwarding borrowed storage to a mutating operation. Use inout when caller-visible mutation is intended, perform the mutation outside the iteration, or limit the operation to reads.
Examples
Mutate a field through a shared borrow
struct Item { value: i32 }
fn change(borrow item: Item) { item.value = 42; }
fn main() -> i32 {
let item = Item { value: 0 };
change(borrow item);
item.value
}
Read through a shared borrow
struct Item { value: i32 }
fn read(borrow item: Item) -> i32 { item.value }
fn main() -> i32 {
let item = Item { value: 42 };
read(borrow item)
}