Skip to content

thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 5' #103632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
markgras opened this issue Oct 27, 2022 · 1 comment
Closed
Labels
C-bug Category: This is a bug.

Comments

@markgras
Copy link

I am currently working through the Rust By Example collection. On the Arrays and Slices page, the last line of the example is supposed to cause an "index out of bounds" compile error when un-commented, but this does not happen. Instead, the code compiles with no error and a thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 5' error shows up at runtime.

Here is a shortened version of this example that erroneously compiles:

fn main() {
    let xs: [i32; 5] = [1, 2, 3, 4, 5];
    for i in 0..xs.len() + 1 {
        match xs.get(i) {
            Some(xval) => println!("{}: {}", i, xval),
            None => println!("Slow down! {} is too far!", i),
        }
    }
    println!("{}", xs[5]);
}

If you remove the for loop then the compiler properly complains. This problem occurs with both the latest stable (1.64.0) and nightly versions. Because this issue exists with nightly, I do not think that it is the same as #103385. Nightly information is given below.

Meta

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc                                        
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19                              
host: x86_64-pc-windows-msvc                         
release: 1.64.0                                      
LLVM version: 14.0.6 
Backtrace

thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 5', main.rs:9:20
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library\std\src\panicking.rs:584
   1: core::panicking::panic_fmt
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library\core\src\panicking.rs:142
   2: core::panicking::panic_bounds_check
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library\core\src\panicking.rs:84
   3: core::fmt::Arguments::new_v1
   4: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@markgras markgras added the C-bug Category: This is a bug. label Oct 27, 2022
@markgras
Copy link
Author

I apologize. Although nightly was installed, it was not my default toolchain (hence why the meta version above is stable). This issue is solved in nightly, so it likely is the same as #103385. I will go ahead and close this issue. Again, I apologize for the spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

1 participant