]> git.lizzy.rs Git - rust.git/blob - src/docs/get_last_with_len.txt
Auto merge of #9425 - kraktus:patch-1, r=xFrednet
[rust.git] / src / docs / get_last_with_len.txt
1 ### What it does
2 Checks for using `x.get(x.len() - 1)` instead of
3 `x.last()`.
4
5 ### Why is this bad?
6 Using `x.last()` is easier to read and has the same
7 result.
8
9 Note that using `x[x.len() - 1]` is semantically different from
10 `x.last()`.  Indexing into the array will panic on out-of-bounds
11 accesses, while `x.get()` and `x.last()` will return `None`.
12
13 There is another lint (get_unwrap) that covers the case of using
14 `x.get(index).unwrap()` instead of `x[index]`.
15
16 ### Example
17 ```
18 let x = vec![2, 3, 5];
19 let last_element = x.get(x.len() - 1);
20 ```
21
22 Use instead:
23 ```
24 let x = vec![2, 3, 5];
25 let last_element = x.last();
26 ```