]> git.lizzy.rs Git - rust.git/blob - src/docs/index_refutable_slice.txt
Auto merge of #9425 - kraktus:patch-1, r=xFrednet
[rust.git] / src / docs / index_refutable_slice.txt
1 ### What it does
2 The lint checks for slice bindings in patterns that are only used to
3 access individual slice values.
4
5 ### Why is this bad?
6 Accessing slice values using indices can lead to panics. Using refutable
7 patterns can avoid these. Binding to individual values also improves the
8 readability as they can be named.
9
10 ### Limitations
11 This lint currently only checks for immutable access inside `if let`
12 patterns.
13
14 ### Example
15 ```
16 let slice: Option<&[u32]> = Some(&[1, 2, 3]);
17
18 if let Some(slice) = slice {
19     println!("{}", slice[0]);
20 }
21 ```
22 Use instead:
23 ```
24 let slice: Option<&[u32]> = Some(&[1, 2, 3]);
25
26 if let Some(&[first, ..]) = slice {
27     println!("{}", first);
28 }
29 ```