E0427: Borrow non lvalue
| Code | Category | Stability |
|---|---|---|
E0427 | Struct and enum | Permanent |
Explanation
A place-required by-reference position does not name addressable storage. This includes implicit receiver autoref, shared or exclusive accessor receivers, and view coercions whose source must remain in place; unlike an ordinary explicit borrow operand, these positions cannot create promoted or temporary storage.
Likely cause
A borrow self method, a borrow or inout accessor, or a place-required view coercion was applied directly to a computed value such as a function or method result. Bind the result to a local before borrowing it. Ordinary explicit borrow function arguments may still use value expressions because those operands are elaborated separately.
Examples
Borrow a computed method receiver
struct Item { value: i32, fn read(borrow self) -> i32 { self.value } }
fn make() -> Item { Item { value: 42 } }
fn main() -> i32 { make().read() }
Bind the receiver to a place
struct Item { value: i32, fn read(borrow self) -> i32 { self.value } }
fn make() -> Item { Item { value: 42 } }
fn main() -> i32 {
let item = make();
item.read()
}