初級 › index out of bounds パニックの原因と直し方 › パターン5
index out of bounds パニックの原因と直し方
存在しない添字でVecや配列にアクセスすると、コンパイルは通りますが実行時にパニックしてプログラムが異常終了します。
1 fn main() { 2 let counts = vec![5, 10, 15, 20, 25]; 3 println!("{}", counts[__]); ^ 4 }
thread 'main' panicked at main.rs:3:26:
index out of bounds: the len is 5 but the index is 9
空欄を埋めて実行を通す
要素数を勘違いしていると、範囲外の添字を指定してしまいパニックの原因になります。
次の問題