@@ -37,32 +37,37 @@ greater than 1 then this requires that the type of `a` is
37
37
>   ;  ; [ _ Expression_ ] ` [ ` [ _ Expression_ ] ` ] `
38
38
39
39
[ 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
+
42
44
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.
46
49
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
50
52
value. Otherwise a check will be performed at run-time that will put the thread
51
53
in a _ panicked state_ if it fails.
52
54
53
55
``` rust,should_panic
56
+ // lint is deny by default.
57
+ #![warn(const_err)]
58
+
54
59
([1, 2, 3, 4])[2]; // Evaluates to 3
55
60
56
61
let b = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
57
62
b[1][2]; // multidimensional array indexing
58
63
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
60
65
61
66
let n = 10;
62
67
let y = (["a", "b"])[n]; // panics
63
68
64
69
let arr = ["a", "b"];
65
- arr[10]; // panics
70
+ arr[10]; // warning: index out of bounds
66
71
```
67
72
68
73
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.
72
77
[ memory location ] : expressions.html#place-expressions-and-value-expressions
73
78
[ Index ] : ../std/ops/trait.Index.html
74
79
[ IndexMut ] : ../std/ops/trait.IndexMut.html
75
- [ constant expression ] : expressions.html#constant-expressions
80
+ [ constant expression ] : expressions.html#constant-expressions
0 commit comments