本页尚未翻译,以下为英文原文。
#E0137: list view used after a resize
A view of a list (a SliceMut or a slice) is used after the list was resized; a resize may move the elements, so the view no longer points at them. Make the view again after the resize, or resize before making it.
nabi
let xs = [1, 2, 3];
let s: collections::SliceMut<i32> = xs[0:2];
xs.push(4);
s[0] = 9;
error[E0137]: `s` views `xs`, which `xs.push(..)` resized at line 3 --> main.nabi:4:1 | 3 | xs.push(4); | ---------- `xs` is resized here 4 | s[0] = 9; | ^ | = help: a resize may move the list's elements, so a view made before it cannot be used after it: make `s` again after the resize (`xs[a:b]` where a `SliceMut` is expected), or resize before making the view
Resize before making the view:
nabi
let xs = [1, 2, 3];
xs.push(4);
let s: collections::SliceMut<i32> = xs[0:2];
s[0] = 9;
println(f"{xs[0]}");
9
A for ref loop over a list walks such a view, so resizing the list in its body and then using the element is this error too: change the list after the loop (3.13.3).
Related: E0105 value used after it was handed over
Found a mistake? Edit this page on GitHub. Last updated