docs

#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에서 이 페이지 고치기. 마지막 수정