初級 › index out of bounds パニックの原因と直し方 › パターン3
index out of bounds パニックの原因と直し方
存在しない添字でVecや配列にアクセスすると、コンパイルは通りますが実行時にパニックしてプログラムが異常終了します。
1 fn main() { 2 let temps = vec![36, 37, 38, 39]; 3 println!("{}", temps[_]); ^ 4 }
thread 'main' panicked at main.rs:3:25:
index out of bounds: the len is 4 but the index is 8
空欄を埋めて実行を通す
要素数ちょうどの添字(この場合4)も範囲外です。最後の要素の添字は要素数より1小さい値になります。
次の問題