Why compiler do not check index out of bounds for Vec?

my code: compile successfully

fn main()
{
    let v : Vec<i32> = Vec::new();

    println!("first element of a vector is :{}",v[0]);
    println!("Second element of a vector is :{}",v[1]);
    println!("Third element of a vector is :{}",v[2]);
    println!("Fourth element of a vector is :{}",v[3]);
}

my running: error

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', src\main.rs:5:49

And I use Clion+Rust plugin to develop. The IDE doesn't show me any info too.

The compiler only prevents UB; it doesn't prevent panics.

It'll help more with arrays, though, since those don't have dynamic length: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9f24319e53b021fbc7a8fc6fcb491c05

2 Likes

Why compiler do not check index out of bounds for Vec?

Why should it? Compile-time checks are all based on types, but Vec<T> is a single type, no matter the length. It's not as powerful as Idris, if that's what you're looking for (it's possible to emulate something like Idris' Vect n, but not fully, not very cleanly and without proper core support).

3 Likes

Vec would have to become part of the language instead of part of the standard library, built with the language. (Indexing is defined by a trait implementation.) Or at least, the compiler would need special knowledge of how Vec works. Even that would only help so much; the length of a Vec isn't always statically determinable.

1 Like

To avoid those panics, you can choose to get (v.get(i)) rather than index (v[i]):

fn main() {
    let v: Vec<i32> = Vec::new();

    if let Some(second) = v.get(1) {
        println!("second element of the vector is: {second}");
    }
}

(See documentation for get and if let.)

5 Likes

the data type that compiler can check is array:

Vector is a mutable at runtime, it's hard to check at compile time.

For arrays the compiler indeed gives a lint error (through the unconditional_panic lint) when the index is known at compile time. If it is computed at runtime, it won't give an error or warning either.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.