]> git.lizzy.rs Git - rust.git/blob - src/docs/len_zero.txt
Add iter_kv_map lint
[rust.git] / src / docs / len_zero.txt
1 ### What it does
2 Checks for getting the length of something via `.len()`
3 just to compare to zero, and suggests using `.is_empty()` where applicable.
4
5 ### Why is this bad?
6 Some structures can answer `.is_empty()` much faster
7 than calculating their length. 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 if x.len() == 0 {
14     ..
15 }
16 if y.len() != 0 {
17     ..
18 }
19 ```
20 instead use
21 ```
22 if x.is_empty() {
23     ..
24 }
25 if !y.is_empty() {
26     ..
27 }
28 ```