#E0137: 串列檢視在調整大小後被使用
串列的檢視(SliceMut 或切片)在串列調整大小之後被使用;調整大小可能會移動元素,所以檢視不再指向它們。在調整大小之後重新建立檢視,或在建立檢視之前先調整大小。
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
在建立檢視之前調整大小:
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
對串列的 for ref 迴圈走訪的就是這樣的檢視,所以在迴圈主體中調整串列大小後再使用該元素,也是這個錯誤:請在迴圈結束後再修改串列(3.13.3)。
相關錯誤碼:E0105 值在移交後被使用
發現錯誤? 在 GitHub 上編輯此頁 最後更新: