Rust by Example; Arrays and Slices; Runtime error, not compile error?

Hello

I am uncertain where to bring this up and the meta sub forum seemed the most suitable place.

Section "2.3 Arrays and Slices" in the Rust by example book finishes its example with these 2 lines:

// Out of bound indexing causes compile error
// println!("{}", xs[5]);

The comment states that the line below would cause a compiler error. I understand form what i see that the issue manifest itself first at runtime and is therefore a runtime error.

Here is what i see:
On my computer, with rustc 1.63.0 (Arch Linux rust 1:1.63.0-1) having the last two lines like this:
// Out of bound indexing causes compile error
println!("{}", xs[5]);

does compile and when run gives me the following output:

first elemnet of the array: 1
second element fo the array 2
number of elements in array: 5
array occupies 20 bytes
borrow the whole array as a slice
first element of the slice: 1
the slive has 5 elements
borrow a section of the array as a slice
first element of the slice: 0
the slive has 3 elements
0: 1
1: 2
2: 3
3: 4
4: 5
Slow down! 5 is to far!
thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 5', 2-3_Arrays_Slices.rs:37:23
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

Ignoring all my typos, the program runs as expected, but ends with what i believe is a runtime error.

Doing the same on the website: Arrays and Slices - Rust By Example gives a similar warning, but printed before the program is run:

thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 5', src/main.rs:54:20
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

OOB indexing of arrays causes a compile error. In the example code you cited, xs is an array. However, OOB indexing of slices causes a runtime error (since the compiler can't know the length of the slice at runtime).

Hello H2CO3

In other words, xs[5] is a slice?

Regardless if xs[5] is a slice or not, the comment states very generally " Out of bound indexing causes compile error". The result running the code does not live up to the expectation the comment gives.

The problem is that the code in the book uses xs.len() between array creation and the problematic line - the same problem as in Out Of Bound compile error with .len() function call - #24 by jbe. This call suppresses the unconditional_panic lint, therefore preventing compilation failure. I'll add this to the corresponding issue.

edit: ah, so it's already there.

2 Likes

Thank you Cerber-Ursi for explaining.

From the bug report I understand that this is an issue with rust and not the book example? I try to learn rust and would be delight to understand what the intended behavior is, if you or anybody knows.

Yes, this is the regression in a relatively recent compiler version. It's intended to work as described in the book, but the lint accidentally allows for more then desired.

Thank you very much for the clarification.