Skip to content

Commit

Permalink
Document bounds checking in the book
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Dec 11, 2015
1 parent 2e48b59 commit 5b9dd6a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/doc/book/vectors.md
Expand Up @@ -61,6 +61,33 @@ error: aborting due to previous error
There’s a lot of punctuation in that message, but the core of it makes sense:
you cannot index with an `i32`.

## Out-of-bounds Access

If you try to access an index that doesn’t exist:

```ignore
let v = vec![1, 2, 3];
println!("Item 7 is {}", v[7]);
```

then the current thread will [panic] with a message like this:

```text
thread '<main>' panicked at 'index out of bounds: the len is 3 but the index is 7'
```

If you want to handle out-of-bounds errors without panicking, you can use
methods like [`get`][get] or [`get_mut`][get_mut] that return `None` when
given an invalid index:

```rust
let v = vec![1, 2, 3];
match v.get(7) {
Some(x) => println!("Item 7 is {}", x),
None => println!("Sorry, this vector is too short.")
}
```

## Iterating

Once you have a vector, you can iterate through its elements with `for`. There
Expand All @@ -87,3 +114,6 @@ API documentation][vec].

[vec]: ../std/vec/index.html
[generic]: generics.html
[panic]: concurrency.html#panics
[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get
[get_mut]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get_mut

0 comments on commit 5b9dd6a

Please sign in to comment.