]> git.lizzy.rs Git - rust.git/blob - src/docs/indexing_slicing.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / indexing_slicing.txt
1 ### What it does
2 Checks for usage of indexing or slicing. Arrays are special cases, this lint
3 does report on arrays if we can tell that slicing operations are in bounds and does not
4 lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
5
6 ### Why is this bad?
7 Indexing and slicing can panic at runtime and there are
8 safe alternatives.
9
10 ### Example
11 ```
12 // Vector
13 let x = vec![0; 5];
14
15 x[2];
16 &x[2..100];
17
18 // Array
19 let y = [0, 1, 2, 3];
20
21 &y[10..100];
22 &y[10..];
23 ```
24
25 Use instead:
26 ```
27
28 x.get(2);
29 x.get(2..100);
30
31 y.get(10);
32 y.get(10..100);
33 ```