#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 上编辑此页 最后更新: