error borrow-escape
Заимствование выходит за допустимые пределы.
What it means#
There are no explicit lifetimes: borrow types (&T, &var T, [T], view T) MUST NOT be stored in fields of data, enum or actors, captured by escaping closures, sent to another task, or appear in Task and stream types. A borrow also must not outlive the borrowed value, and a function may return a borrow only if it comes from self or from the only borrowed parameter (§7.2).
Example#
data Holder {
items: &List<i32> // <- borrow-escape
}borrow-escape
How to fix#
data Holder {
items: List<i32>
}Store an owned value (move it in, or .clone() it), or share through Arc<frozen T> when several owners are needed.
Zero-copy views (view T, §9.6)#
decode_view<T>(&buf) returns a view T that borrows buf: it follows the same rules. Returning a view of a local
buffer is the usual mistake:
fn parse() -> view Order {
let buf = receive()
decode_view<Order>(&buf)! // <- borrow-escape: `buf` does not outlive the function
}Take the buffer as a borrowed parameter (fn parse(buf: &bytes) -> view Order), or return an owned value with
v.own() (fn parse() -> Order). A view T also cannot be stored in a field, captured by an escaping closure or sent
to another task; an rpc parameter rpc f(o: view T) is the exception, the runtime keeps its buffer alive for the
handler (D-6504..D-6506).