E0497: Str view not first class
| Code | Category | Stability |
|---|---|---|
E0497 | Struct and enum | Permanent |
Explanation
The binding of a borrow str or inout str parameter is a second-class view valid only for that call. It may be read or re-borrowed, but converting it to a first-class str by returning, storing, or rebinding it would let the view escape the lifetime of the borrowed buffer.
Likely cause
A function returns a borrowed string parameter as str, stores it in a str field, or passes it to a bare str parameter. Keep all use within the borrow, re-borrow for another view parameter, or produce a separate owning value instead.
Examples
Return a borrowed string view
fn escape(borrow value: str) -> str { value }
fn main() -> i32 {
let value: Str(5) = "hello";
let escaped = escape(borrow value);
@intCast(escaped.len())
}
Read the string view within its call
fn length(borrow value: str) -> u64 { value.len() }
fn main() -> i32 {
let value: Str(8) = "hello";
@intCast(length(borrow value))
}