Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Out of bound for vectors

  • Run-time panic
#[allow(clippy::useless_vec)]
fn main() {
    let v = vec!["apple", "banana", "peach"];
    println!("{}", v[0]);
    println!("{}", v[5]);
}
apple
thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 5', examples/errors/out_of_bounds_vector.rs:4:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace