mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-26 09:18:42 +08:00
Move explanation up as per @aturon
This commit is contained in:
parent
80c97251b8
commit
730fb1f82f
@ -39,7 +39,12 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
We attempt to access the hundredth element of our vector, but it only has three
|
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
|
```bash
|
||||||
$ cargo run
|
$ 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:
|
That's a lot of output! Line `11` there has the line in our project:
|
||||||
`src/main.rs` line four.
|
`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.
|
|
||||||
|
Loading…
Reference in New Issue
Block a user