Skip to content

Commit 82fd726

Browse files
committed
Update array expressions
* const_err is now deny-by-default * Indexes are checked on non-const arrays. * Fix some typos
1 parent 320c8e7 commit 82fd726

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/expressions/array-expr.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,37 @@ greater than 1 then this requires that the type of `a` is
3737
>    [_Expression_] `[` [_Expression_] `]`
3838
3939
[Array and slice](types.html#array-and-slice-types)-typed expressions can be
40-
indexed by writing a square-bracket-enclosed expression (the index) after them.
41-
When the array is mutable, the resulting [memory location] can be assigned to.
40+
indexed by writing a square-bracket-enclosed expression of type `usize` (the
41+
index) after them. When the array is mutable, the resulting [memory location]
42+
can be assigned to.
43+
4244
For other types an index expression `a[b]` is equivalent to
43-
`*std::ops::Index::index(&a, b)`, or `*std::opsIndexMut::index_mut(&mut a, b)`
44-
in a mutable place expression context. Just as with methods, Rust will also
45-
insert dereference operations on `a` repeatedly to find an implementation.
45+
`*std::ops::Index::index(&a, b)`, or
46+
`*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression
47+
context. Just as with methods, Rust will also insert dereference operations on
48+
`a` repeatedly to find an implementation.
4649

47-
Indices are zero-based, and are of type `usize` for arrays and slices. Array
48-
access is a [constant expression], so
49-
bounds can be checked at compile-time for constant arrays with a constant index
50+
Indices are zero-based for arrays and slices. Array access is a [constant
51+
expression], so bounds can be checked at compile-time with a constant index
5052
value. Otherwise a check will be performed at run-time that will put the thread
5153
in a _panicked state_ if it fails.
5254

5355
```rust,should_panic
56+
// lint is deny by default.
57+
#![warn(const_err)]
58+
5459
([1, 2, 3, 4])[2]; // Evaluates to 3
5560
5661
let b = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
5762
b[1][2]; // multidimensional array indexing
5863
59-
let x = (["a", "b"])[10]; // warning: const index-expr is out of bounds
64+
let x = (["a", "b"])[10]; // warning: index out of bounds
6065
6166
let n = 10;
6267
let y = (["a", "b"])[n]; // panics
6368
6469
let arr = ["a", "b"];
65-
arr[10]; // panics
70+
arr[10]; // warning: index out of bounds
6671
```
6772

6873
The array index expression can be implemented for types other than arrays and slices
@@ -72,4 +77,4 @@ by implementing the [Index] and [IndexMut] traits.
7277
[memory location]: expressions.html#place-expressions-and-value-expressions
7378
[Index]: ../std/ops/trait.Index.html
7479
[IndexMut]: ../std/ops/trait.IndexMut.html
75-
[constant expression]: expressions.html#constant-expressions
80+
[constant expression]: expressions.html#constant-expressions

0 commit comments

Comments
 (0)