]> git.lizzy.rs Git - rust.git/blob - src/docs/comparison_to_empty.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / comparison_to_empty.txt
1 ### What it does
2 Checks for comparing to an empty slice such as `""` or `[]`,
3 and suggests using `.is_empty()` where applicable.
4
5 ### Why is this bad?
6 Some structures can answer `.is_empty()` much faster
7 than checking for equality. So it is good to get into the habit of using
8 `.is_empty()`, and having it is cheap.
9 Besides, it makes the intent clearer than a manual comparison in some contexts.
10
11 ### Example
12
13 ```
14 if s == "" {
15     ..
16 }
17
18 if arr == [] {
19     ..
20 }
21 ```
22 Use instead:
23 ```
24 if s.is_empty() {
25     ..
26 }
27
28 if arr.is_empty() {
29     ..
30 }
31 ```