Skip to content

Commit e724bd8

Browse files
authored
Merge pull request #2566 from rust-lang/outsmarting-the-smartypants-compiler
Fixes #2417. Get the index from user input instead of a const.
2 parents f0c83d3 + 79bafdc commit e724bd8

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
use std::io;
2+
13
fn main() {
24
let a = [1, 2, 3, 4, 5];
3-
let index = 10;
5+
6+
println!("Please enter an array index.");
7+
8+
let mut index = String::new();
9+
10+
io::stdin()
11+
.read_line(&mut index)
12+
.expect("Failed to read line");
13+
14+
let index: usize = index
15+
.trim()
16+
.parse()
17+
.expect("Index entered was not a number");
418

519
let element = a[index];
620

7-
println!("The value of element is: {}", element);
21+
println!(
22+
"The value of the element at index {} is: {}",
23+
index, element
24+
);
825
}

src/ch03-02-data-types.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,26 +324,39 @@ get the value `2` from index `[1]` in the array.
324324
##### Invalid Array Element Access
325325

326326
What happens if you try to access an element of an array that is past the end
327-
of the array? Say you change the example to the following code, which will
328-
compile but exit with an error when it runs:
327+
of the array? Say you change the example to the following, which uses code
328+
similar to the guessing game in Chapter 2 to get an array index from the user:
329329

330330
<span class="filename">Filename: src/main.rs</span>
331331

332332
```rust,ignore,does_not_compile
333333
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs}}
334334
```
335335

336-
Running this code using `cargo run` produces the following result:
336+
This code compiles successfully. If you run this code using `cargo run` and
337+
enter 0, 1, 2, 3, or 4, the program will print out the corresponding value at
338+
that index in the array. If you instead enter a number past the end of the
339+
array, such as 10, you'll see output like this:
340+
341+
<!-- manual-regeneration
342+
cd listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access
343+
cargo run
344+
10
345+
-->
337346

338347
```console
339-
{{#include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt}}
348+
thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:19:19
349+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
340350
```
341351

342-
The compilation didn’t produce any errors, but the program resulted in a
343-
*runtime* error and didn’t exit successfully. When you attempt to access an
352+
The program resulted in a *runtime* error at the point of using an invalid
353+
value in the indexing operation. The program exited at that point with an error
354+
message and didn't execute the final `println!`. When you attempt to access an
344355
element using indexing, Rust will check that the index you’ve specified is less
345356
than the array length. If the index is greater than or equal to the array
346-
length, Rust will panic.
357+
length, Rust will panic. This check has to happen at runtime, especially in
358+
this case, because the compiler can't possibly know what the value a user
359+
running the code will later enter.
347360

348361
This is the first example of Rust’s safety principles in action. In many
349362
low-level languages, this kind of check is not done, and when you provide an

0 commit comments

Comments
 (0)