E0489: Slice escapes scope
| Code | Category | Stability |
|---|---|---|
E0489 | Struct and enum | Permanent |
Explanation
A slice [T] may name only a function parameter. Giving a local or constant binding slice type would extend a second-class borrowed view beyond the argument scope whose access rules keep the aliased storage valid.
Likely cause
A let or const declaration has an explicit [T] annotation, commonly to preserve a view received by a function. Keep the view in its parameter binding and use it there, or bind an owning array or container instead.
Examples
Bind a slice to a local
fn main() -> i32 {
let values = [20, 22];
let view: [i32] = values;
view[0]
}
Read a slice parameter directly
fn sum(borrow values: [i64]) -> i32 { @intCast(values[0] + values[1]) }
fn main() -> i32 {
let values: [i64; 2] = [20, 22];
sum(borrow values)
}