diff --git a/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt b/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt index 273b34711a..ee1c62fd29 100644 --- a/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt +++ b/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt @@ -1,4 +1,4 @@ -$ cargo run +$ cargo build Compiling arrays v0.1.0 (file:///projects/arrays) error: this operation will panic at runtime --> src/main.rs:5:19 diff --git a/src/ch03-02-data-types.md b/src/ch03-02-data-types.md index 62111c7241..ac3c8e62e2 100644 --- a/src/ch03-02-data-types.md +++ b/src/ch03-02-data-types.md @@ -315,8 +315,7 @@ get the value `2` from index `[1]` in the array. ##### Invalid Array Element Access What happens if you try to access an element of an array that is past the end -of the array? Say you change the example to the following code, which will -compile but exit with an error when it runs: +of the array? If you change the example to the following code, it will not compile: Filename: src/main.rs @@ -324,23 +323,22 @@ compile but exit with an error when it runs: {{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs}} ``` -Running this code using `cargo run` produces the following result: +Running this code using `cargo build` produces the following result: ```console {{#include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/output.txt}} ``` -The compilation didn’t produce any errors, but the program resulted in a -*runtime* error and didn’t exit successfully. When you attempt to access an -element using indexing, Rust will check that the index you’ve specified is less -than the array length. If the index is greater than or equal to the array -length, Rust will panic. +When you attempt to compile code that has invalid array access, The rust compiler +will catch that and will not allow for a successful compilation. This is the first example of Rust’s safety principles in action. In many low-level languages, this kind of check is not done, and when you provide an incorrect index, invalid memory can be accessed. Rust protects you against this -kind of error by immediately exiting instead of allowing the memory access and -continuing. Chapter 9 discusses more of Rust’s error handling. +kind of error by not allowing the program to compile, or it might panic at *runtime* +if the error could not identified at *compile time*, which would immediately exit the program +instead of allowing the invalid memory access to occur. +Chapter 9 discusses more of Rust’s error handling. [comparing-the-guess-to-the-secret-number]: ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number