From 730fb1f82f0b3d33b2c09e45a66b6f84828130b7 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 25 Aug 2016 16:32:36 -0400 Subject: [PATCH] Move explanation up as per @aturon --- src/ch07-02-unrecoverable-errors-with-panic.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ch07-02-unrecoverable-errors-with-panic.md b/src/ch07-02-unrecoverable-errors-with-panic.md index fa045a5..c966eea 100644 --- a/src/ch07-02-unrecoverable-errors-with-panic.md +++ b/src/ch07-02-unrecoverable-errors-with-panic.md @@ -39,7 +39,12 @@ fn main() { ``` We attempt to access the hundredth element of our vector, but it only has three -elements. In this situation, Rust will panic. Let's try it: +elements. In this situation, Rust will panic. Using `[]` is supposed to return +a number. But if you pass it an invalid index, there's no number Rust could +return here, it would be wrong. So the only thing that we can do is terminate +the program. + +Let's try to run it: ```bash $ cargo run @@ -97,8 +102,3 @@ error: Process didn't exit successfully: `target/debug/panic` (exit code: 101) That's a lot of output! Line `11` there has the line in our project: `src/main.rs` line four. - -So why does Rust panic here? In this case, using `[]` is supposed to return -a number. But if you pass it an invalid index, there's no number Rust could -return here, it would be wrong. So the only thing that we can do is terminate -the program.