Skip to content

Commit 295b62d

Browse files
author
Johann Hofmann
committed
Docs: Compile-time bounds check in index expression
The reference was claiming all vectors all bounds-checked at run-time, when constant vectors are usually checked at compile-time. For the changed example see http://is.gd/28ak9E
1 parent 9ecc989 commit 295b62d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/doc/reference.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ module declarations should be at the crate root if direct usage of the declared
842842
modules within `use` items is desired. It is also possible to use `self` and
843843
`super` at the beginning of a `use` item to refer to the current and direct
844844
parent modules respectively. All rules regarding accessing declared modules in
845-
`use` declarations applies to both module declarations and `extern crate`
845+
`use` declarations apply to both module declarations and `extern crate`
846846
declarations.
847847

848848
An example of what will and will not work for `use` items:
@@ -2564,12 +2564,19 @@ array is mutable, the resulting [lvalue](#lvalues,-rvalues-and-temporaries) can
25642564
be assigned to.
25652565

25662566
Indices are zero-based, and may be of any integral type. Vector access is
2567-
bounds-checked at run-time. When the check fails, it will put the thread in a
2568-
_panicked state_.
2567+
bounds-checked at compile-time for constant arrays being accessed with a constant index value.
2568+
Otherwise a check will be performed at run-time that will put the thread in a _panicked state_ if it fails.
25692569

25702570
```{should-fail}
25712571
([1, 2, 3, 4])[0];
2572-
(["a", "b"])[10]; // panics
2572+
2573+
let x = (["a", "b"])[10]; // compiler error: const index-expr is out of bounds
2574+
2575+
let n = 10;
2576+
let y = (["a", "b"])[n]; // panics
2577+
2578+
let arr = ["a", "b"];
2579+
arr[10]; // panics
25732580
```
25742581

25752582
### Range expressions

0 commit comments

Comments
 (0)